cz.imakers.android.divky.SettingsActivity.java Source code

Java tutorial

Introduction

Here is the source code for cz.imakers.android.divky.SettingsActivity.java

Source

package cz.imakers.android.divky;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.support.v4.content.LocalBroadcastManager;
import android.view.MenuItem;
import android.webkit.WebView;

public class SettingsActivity extends PreferenceActivity {

    private final IntentFilter filter = new IntentFilter(DownloadService.ACTION);
    private boolean isReceiverRegistered = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setDisplayShowTitleEnabled(true);
        getActionBar().setTitle(R.string.settings);
    }

    @SuppressWarnings("deprecation")
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
        Preference restore = findPreference("pref_restore");
        Preference about = findPreference("pref_about");
        restore.setOnPreferenceClickListener(new OnPreferenceClickListener() {

            @Override
            public boolean onPreferenceClick(Preference preference) {
                AlertDialog.Builder builder = new AlertDialog.Builder(preference.getContext());
                builder.setTitle(R.string.restore_title).setMessage(R.string.restore_info)
                        .setIcon(android.R.drawable.ic_dialog_info).setNegativeButton(android.R.string.cancel, null)
                        .setPositiveButton(android.R.string.ok, new OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                DownloadQueue.getInstance().needRestore = true;
                                dialog.dismiss();
                                onBackPressed();
                            }
                        });
                builder.create().show();
                return true;
            }
        });
        // Get app version
        PackageManager pm = getPackageManager();
        String packageName = getPackageName();
        String versionName;
        try {
            PackageInfo info = pm.getPackageInfo(packageName, 0);
            versionName = info.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            versionName = "N/A";
            e.printStackTrace();
        }
        about.setSummary(getResources().getString(R.string.app_name) + " ver. " + versionName);
        about.setOnPreferenceClickListener(new OnPreferenceClickListener() {

            @Override
            public boolean onPreferenceClick(Preference preference) {
                AlertDialog.Builder builder = new AlertDialog.Builder(preference.getContext());
                WebView webView = new WebView(preference.getContext());
                webView.loadUrl("file:///android_asset/LICENSE.html");
                builder.setView(webView).setTitle(R.string.about).setIcon(android.R.drawable.ic_dialog_info)
                        .setNeutralButton(android.R.string.ok, null).create().show();
                return true;
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            onBackPressed();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    @Override
    protected void onResume() {
        LocalBroadcastManager.getInstance(this).registerReceiver(receiver, filter);
        isReceiverRegistered = true;
        super.onResume();
    }

    @Override
    protected void onPause() {
        if (isReceiverRegistered) {
            LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
            isReceiverRegistered = false;
        }
        super.onPause();
    }

    private BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, final Intent intent) {
            int resultCode = intent.getIntExtra(DownloadService.TAG_CODE, 0);
            int resultValue = intent.getIntExtra(DownloadService.TAG_RESULT, 0);
            int position = intent.getIntExtra(DownloadService.TAG_POS, 0);
            boolean all_prev = intent.getBooleanExtra(DownloadService.TAG_ALL_PREV, false);
            if (all_prev) {
                switch (resultCode) {
                case DownloadService.UNZIP:
                    if (resultValue == 100) {
                        DownloadQueue.getInstance().removeFirst();
                        GirlsDbHelper.getInstance(context).offFragNotified = false;
                        GirlsDbHelper.getInstance(context).onFragNotified = false;
                    }
                    break;
                }
            }
        }
    };
}