com.klinker.android.twitter.settings.SettingsActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.klinker.android.twitter.settings.SettingsActivity.java

Source

/*
 * Copyright 2014 Luke Klinker
 *
 * 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.
 */

package com.klinker.android.twitter.settings;

import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.text.Spanned;
import android.util.Log;
import android.view.*;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.ListView;

import android.widget.Toast;
import com.klinker.android.launcher.api.BaseLauncherPage;
import com.klinker.android.twitter.R;
import com.klinker.android.twitter.adapters.ChangelogAdapter;
import com.klinker.android.twitter.ui.MainActivity;
import com.klinker.android.twitter.manipulations.widgets.HoloTextView;
import com.klinker.android.twitter.utils.Utils;
import com.klinker.android.twitter.utils.XmlChangelogUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class SettingsActivity extends Activity {

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.activity_zoom_enter, R.anim.slide_out_right);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        overridePendingTransition(R.anim.slide_in_left, R.anim.activity_zoom_exit);

        AppSettings.invalidate();

        Utils.setUpTheme(this, AppSettings.getInstance(this));

        ActionBar ab = getActionBar();
        ab.setDisplayHomeAsUpEnabled(true);
        ab.setDisplayShowHomeEnabled(true);

        getFragmentManager().beginTransaction().replace(android.R.id.content, new MainPrefFrag()).commit();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_settings, menu);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            onBackPressed();
            return true;
        case R.id.menu_whats_new:
            final Context context = this;
            final ListView list = new ListView(this);
            list.setDividerHeight(0);

            new AsyncTask<Spanned[], Void, Spanned[]>() {
                @Override
                public Spanned[] doInBackground(Spanned[]... params) {
                    return XmlChangelogUtils.parse(context);
                }

                @Override
                public void onPostExecute(Spanned[] result) {
                    list.setAdapter(new ChangelogAdapter(context, result));
                }
            }.execute();

            new AlertDialog.Builder(this).setTitle(R.string.changelog).setView(list)
                    .setPositiveButton(R.string.ok, null).show();
            return true;
        case R.id.menu_rate_it:
            Uri uri = Uri.parse("market://details?id=" + getPackageName());
            Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);

            try {
                startActivity(goToMarket);
            } catch (ActivityNotFoundException e) {
                Toast.makeText(this, "Couldn't launch the market", Toast.LENGTH_SHORT).show();
            }
            return true;
        case R.id.menu_get_help:
            showSettings(7, getString(R.string.get_help_settings));
            return true;
        case R.id.menu_other_apps:
            showSettings(8, getString(R.string.other_apps));
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onBackPressed() {
        AppSettings.invalidate();
        Intent main = new Intent(this, MainActivity.class);
        startActivity(main);
        finish();
    }

    private void showSettings(int position, String title) {
        startActivity(new Intent(this, PrefActivity.class).putExtra("position", position).putExtra("title", title));
    }
}