com.brandao.tictactoe.settings.SettingsActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.brandao.tictactoe.settings.SettingsActivity.java

Source

/*
 * Copyright (c) 2016 Skytrait
 *
 * Licensed under the Apache License, Version 2.0 (the License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * www.skytrait.com
 *
 */

package com.brandao.tictactoe.settings;

import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.app.NavUtils;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import com.brandao.tictactoe.R;

public class SettingsActivity extends AppCompatActivity {
    public static final String TAG = "SettingsActivity";

    private final String FRAGMENT_TAG_SETTINGS = "settings";

    public static final int ACTIVITY_RESULT_SETTINGS_UPDATE = 1;
    public static final int ACTIVITY_RESULT_SETTINGS_ADVERTISEMENT_UPDATE = 3;

    public static final int ACTIVITY_REQUEST_SETTINGS = 2;

    private SettingsFragment mSettingsFragment;
    private Toolbar mToolbar;
    private CoordinatorLayout mCoordinatorLayout;

    private SharedPreferences mPrefs;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_acitivty);

        mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
        mCoordinatorLayout = (CoordinatorLayout) findViewById(R.id.settings_activity_coordinator_layout);

        mToolbar = (Toolbar) findViewById(R.id.settings_activity_tool_bar);
        setSupportActionBar(mToolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        mSettingsFragment = (SettingsFragment) getFragmentManager().findFragmentByTag(FRAGMENT_TAG_SETTINGS);
        if (mSettingsFragment == null) {
            mSettingsFragment = SettingsFragment.newInstance();

            getFragmentManager().beginTransaction()
                    .replace(R.id.settings_fragment_container, mSettingsFragment, FRAGMENT_TAG_SETTINGS).commit();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == ACTIVITY_REQUEST_SETTINGS) {
            setResult(resultCode, data);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.settings_activity, menu);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home: {
            NavUtils.navigateUpFromSameTask(this);

            return true;
        }
        case R.id.menu_item_app_manager: {
            Intent intent = new Intent();
            intent.setAction(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            Uri uri = Uri.fromParts("package", getApplicationContext().getPackageName(), null);
            intent.setData(uri);

            startActivity(intent);

            return true;
        }
        }

        return super.onOptionsItemSelected(item);
    }

    public CoordinatorLayout getCoordinatorLayout() {
        return mCoordinatorLayout;
    }

    @Override
    public void finish() {
        Settings.sendPrefAnalyticEvent(this, mPrefs);

        super.finish();
    }
}