Android Open Source - callmeter Preferences Plain






From Project

Back to project page callmeter.

License

The source code is released under:

GNU General Public License

If you think the Android project callmeter listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2009-2013 Felix Bechstein
 * //from  ww w  . j a  v  a2 s . c om
 * This file is part of Call Meter 3G.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; If not, see <http://www.gnu.org/licenses/>.
 */
package de.ub0r.android.callmeter.ui.prefs;

import org.json.JSONException;
import org.json.JSONObject;

import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceGroup;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

import de.ub0r.android.callmeter.R;
import de.ub0r.android.callmeter.data.ExportProvider;
import de.ub0r.android.callmeter.ui.HelpActivity;
import de.ub0r.android.callmeter.ui.TrackingSherlockPreferenceActivity;
import de.ub0r.android.callmeter.widget.LogsAppWidgetConfigure;
import de.ub0r.android.callmeter.widget.LogsAppWidgetProvider;
import de.ub0r.android.callmeter.widget.StatsAppWidgetConfigure;
import de.ub0r.android.callmeter.widget.StatsAppWidgetProvider;
import de.ub0r.android.logg0r.Log;
import de.ub0r.android.lib.Utils;

/**
 * Show simple preferences.
 *
 * @author flx
 */
public final class PreferencesPlain extends TrackingSherlockPreferenceActivity implements
        OnPreferenceClickListener, OnPreferenceChangeListener {

    private static final String TAG = "PreferencesPlain";

    /** Action. */
    private static final String APPERANCE = "APPERANCE";
    /** Action. */
    private static final String TEXTSIZE = "TEXTSIZE";
    /** Action. */
    private static final String BEHAVIOR = "BEHAVIOR";
    /** Action. */
    private static final String ALERT = "ALERT";
    /** Action. */
    private static final String ASK_FOR_PLAN = "ASK_FOR_PLAN";
    /** Action. */
    private static final String WIDGETS = "WIDGETS";
    /** Action. */
    private static final String EXPORT = "EXPORT";
    /** Action. */
    private static final String IMPORT = "IMPORT";
    /** Action. */
    private static final String ADVANCED = "ADVANCED";

    private static final long CACHE_TIMEOUT = 1000L * 60L * 15L; // 15min

    private void loadRules() {
        new AsyncTask<Void, Void, JSONObject>() {
            @Override
            protected JSONObject doInBackground(final Void... params) {
                try {
                    String l;
                    StringBuilder sb = new StringBuilder();

                    File f = new File(PreferencesPlain.this.getCacheDir(), "ub0rrules.json");
                    if (f.exists() && f.lastModified() + CACHE_TIMEOUT > System
                            .currentTimeMillis()) {
                        Log.i(TAG, "found cached data: " + f.getAbsolutePath());
                        BufferedReader r = new BufferedReader(new FileReader(f));
                        while ((l = r.readLine()) != null) {
                            sb.append(l);
                        }
                        r.close();
                    }
                    if (sb.length() == 0) {
                        if (f.exists()) {
                            f.delete();
                        }
                        HttpURLConnection c = (HttpURLConnection) new URL(
                                "https://raw.githubusercontent.com/felixb/callmeter/gh-pages/_data/rulesets.json")
                                .openConnection();
                        Log.i(TAG, "load new data: " + c.getURL());
                        BufferedReader r = new BufferedReader(new InputStreamReader(
                                c.getInputStream()));
                        FileWriter w = new FileWriter(f);
                        while ((l = r.readLine()) != null) {
                            sb.append(l);
                            w.write(l);
                        }
                        r.close();
                        w.close();
                    }
                    try {
                        return new JSONObject(sb.toString());
                    } catch (JSONException e) {
                        Log.e(TAG, "JSON Error", e);
                        Log.e(TAG, "JSON: " + sb.toString());
                        return null;
                    }
                } catch (IOException e) {
                    Log.e(TAG, "IOError", e);
                }
                return null;
            }

            @SuppressWarnings({"deprecation", "rawtypes"})
            @Override
            protected void onPostExecute(final JSONObject result) {
                if (result == null) {
                    Toast.makeText(PreferencesPlain.this, R.string.err_export_read,
                            Toast.LENGTH_LONG).show();
                    return;
                }
                PreferenceGroup base = (PreferenceGroup) PreferencesPlain.this
                        .findPreference("import_rules_users");
                PreferenceManager pm = base.getPreferenceManager();
                // delete old
                base.removeAll();
                base = (PreferenceGroup) PreferencesPlain.this.findPreference("import_rules_base");
                // build list
                ArrayList<String> keys = new ArrayList<String>(result.length());

                Iterator it = result.keys();
                while (it.hasNext()) {
                    keys.add(it.next().toString());
                }
                Collections.sort(keys);
                keys.remove("common");
                keys.add(0, "common");

                OnPreferenceClickListener opcl = new OnPreferenceClickListener() {
                    @Override
                    public boolean onPreferenceClick(final Preference preference) {
                        Intent intent = new Intent(PreferencesPlain.this, PreferencesRules.class);
                        intent.putExtra(PreferencesRules.EXTRA_JSON, result.toString());
                        intent.putExtra(PreferencesRules.EXTRA_COUNTRY, preference.getKey());
                        PreferencesPlain.this.startActivity(intent);
                        return true;
                    }
                };

                for (String k : keys) {
                    PreferenceScreen p = pm.createPreferenceScreen(PreferencesPlain.this);
                    p.setPersistent(false);
                    p.setKey(k);
                    p.setTitle(k);
                    p.setOnPreferenceClickListener(opcl);
                    base.addPreference(p);
                }
            }

            ;
        }.execute((Void) null);
    }

    @SuppressWarnings("deprecation")
    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Utils.setLocale(this);

        String a = getIntent().getAction();
        if (a == null) {
            finish();
            return;
        } else if (APPERANCE.equals(a)) {
            addPreferencesFromResource(R.xml.prefs_apperance);
        } else if (TEXTSIZE.equals(a)) {
            addPreferencesFromResource(R.xml.prefs_apperance_textsize);
        } else if (BEHAVIOR.equals(a)) {
            addPreferencesFromResource(R.xml.prefs_behavior);
        } else if (ALERT.equals(a)) {
            addPreferencesFromResource(R.xml.prefs_behavior_alert);
        } else if (ASK_FOR_PLAN.equals(a)) {
            addPreferencesFromResource(R.xml.prefs_behavior_askforplan);
        } else if (WIDGETS.equals(a)) {
            addPreferencesFromResource(R.xml.group_prefs);
        } else if (IMPORT.equals(a)) {
            addPreferencesFromResource(R.xml.prefs_import);
            findPreference("import_rules_default").setOnPreferenceClickListener(this);
            loadRules();
        } else if (EXPORT.equals(a)) {
            addPreferencesFromResource(R.xml.prefs_export);
            findPreference("export_rules").setOnPreferenceClickListener(this);
            findPreference("export_rules_sd").setOnPreferenceClickListener(this);
            findPreference("export_rules_dev").setOnPreferenceClickListener(this);
            findPreference("export_logs").setOnPreferenceClickListener(this);
            findPreference("export_logs_csv").setOnPreferenceClickListener(this);
            findPreference("export_numgroups").setOnPreferenceClickListener(this);
            findPreference("export_hourgroups").setOnPreferenceClickListener(this);
        } else if (ADVANCED.equals(a)) {
            addPreferencesFromResource(R.xml.prefs_advanced);
            findPreference(Preferences.PREFS_ADVANCED).setOnPreferenceChangeListener(this);
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        String a = getIntent().getAction();
        if (a == null) {
            finish();
            return;
        } else if (WIDGETS.equals(a)) {
            loadWidgets();
        }
    }

    /** Load widget list. */
    @SuppressWarnings("deprecation")
    private void loadWidgets() {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        Preference p = findPreference("container");
        if (p != null && p instanceof PreferenceScreen) {
            PreferenceScreen ps = (PreferenceScreen) p;
            ps.removeAll();
            int[] ids = AppWidgetManager.getInstance(this).getAppWidgetIds(
                    new ComponentName(this, StatsAppWidgetProvider.class));
            boolean added = false;
            if (ids != null && ids.length > 0) {
                for (int id : ids) {
                    if (prefs.getLong(StatsAppWidgetProvider.WIDGET_PLANID + id, -1) <= 0) {
                        continue;
                    }
                    added = true;
                    p = new Preference(this);
                    p.setTitle(getString(R.string.widget_) + " #" + id);
                    final int fid = id;
                    p.setOnPreferenceClickListener(new OnPreferenceClickListener() {
                        @Override
                        public boolean onPreferenceClick(final Preference preference) {
                            Intent i = new Intent(PreferencesPlain.this,
                                    StatsAppWidgetConfigure.class);
                            i.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, fid);
                            PreferencesPlain.this.startActivity(i);
                            return true;
                        }
                    });
                    ps.addPreference(p);
                }
            }
            ids = AppWidgetManager.getInstance(this).getAppWidgetIds(
                    new ComponentName(this, LogsAppWidgetProvider.class));
            if (ids != null && ids.length > 0) {
                for (int id : ids) {
                    if (prefs.getLong(LogsAppWidgetProvider.WIDGET_PLANID + id, -1) <= 0) {
                        continue;
                    }
                    added = true;
                    p = new Preference(this);
                    p.setTitle(getString(R.string.widget_) + " #" + id);
                    final int fid = id;
                    p.setOnPreferenceClickListener(new OnPreferenceClickListener() {
                        @Override
                        public boolean onPreferenceClick(final Preference preference) {
                            Intent i = new Intent(PreferencesPlain.this,
                                    LogsAppWidgetConfigure.class);
                            i.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, fid);
                            PreferencesPlain.this.startActivity(i);
                            return true;
                        }
                    });
                    ps.addPreference(p);
                }
            }
            if (!added) {
                p = new Preference(this);
                p.setTitle(R.string.widgets_no_widgets_);
                p.setSummary(R.string.widgets_no_widgets_hint);
                ps.addPreference(p);
            }
        }
    }

    @Override
    public boolean onPreferenceClick(final Preference preference) {
        final String k = preference.getKey();
        if (k.equals("export_rules")) {
            Preferences
                    .exportData(this, null, null, null, ExportProvider.EXPORT_RULESET_FILE, null);
            return true;
        } else if (k.equals("export_rules_sd")) {
            Preferences.exportData(this, null, null, null, ExportProvider.EXPORT_RULESET_FILE,
                    "sdcard");
            return true;
        } else if (k.equals("export_rules_dev")) {
            Preferences.exportData(this, null, null, null, ExportProvider.EXPORT_RULESET_FILE,
                    "android@ub0r.de");
            return true;
        } else if (k.equals("export_logs")) {
            Preferences.exportData(this, null, null, null, ExportProvider.EXPORT_LOGS_FILE, null);
            return true;
        } else if (k.equals("export_logs_csv")) {
            Preferences.exportLogsCsv(this);
            return true;
        } else if (k.equals("export_numgroups")) {
            Preferences.exportData(this, null, null, null, ExportProvider.EXPORT_NUMGROUPS_FILE,
                    null);
            return true;
        } else if (k.equals("export_hourgroups")) {
            Preferences.exportData(this, null, null, null, ExportProvider.EXPORT_HOURGROUPS_FILE,
                    null);
            return true;
        } else if (k.equals("import_rules_default")) {
            final Intent i = new Intent(this, Preferences.class);
            i.setData(Uri.parse("content://default"));
            startActivity(i);
            return true;
        }
        return false;
    }

    @Override
    public boolean onPreferenceChange(final Preference preference, final Object newValue) {
        final String k = preference.getKey();
        if (k.equals(Preferences.PREFS_ADVANCED)) {
            if (newValue.equals(true)) {
                startActivity(new Intent(this, HelpActivity.class));
            }
            return true;
        }
        return false;
    }

}




Java Source Code List

android.preference.DatePreference.java
com.actionbarsherlock.BuildConfig.java
de.ub0r.android.callmeter.Ads.java
de.ub0r.android.callmeter.CallMeter.java
de.ub0r.android.callmeter.TrackingUtils.java
de.ub0r.android.callmeter.data.DataProvider.java
de.ub0r.android.callmeter.data.Device.java
de.ub0r.android.callmeter.data.ExportProvider.java
de.ub0r.android.callmeter.data.LogRunnerReceiver.java
de.ub0r.android.callmeter.data.LogRunnerService.java
de.ub0r.android.callmeter.data.NameCache.java
de.ub0r.android.callmeter.data.NameLoader.java
de.ub0r.android.callmeter.data.RuleMatcher.java
de.ub0r.android.callmeter.data.SysClassNet.java
de.ub0r.android.callmeter.ui.AboutActivity.java
de.ub0r.android.callmeter.ui.AddLogActivity.java
de.ub0r.android.callmeter.ui.AskForPlan.java
de.ub0r.android.callmeter.ui.Common.java
de.ub0r.android.callmeter.ui.HelpActivity.java
de.ub0r.android.callmeter.ui.IntroActivity.java
de.ub0r.android.callmeter.ui.LogsFragment.java
de.ub0r.android.callmeter.ui.PlansFragment.java
de.ub0r.android.callmeter.ui.Plans.java
de.ub0r.android.callmeter.ui.TrackingActivity.java
de.ub0r.android.callmeter.ui.TrackingSherlockActivity.java
de.ub0r.android.callmeter.ui.TrackingSherlockFragmentActivity.java
de.ub0r.android.callmeter.ui.TrackingSherlockPreferenceActivity.java
de.ub0r.android.callmeter.ui.prefs.BillModeListPreference.java
de.ub0r.android.callmeter.ui.prefs.CV2EditTextPreference.java
de.ub0r.android.callmeter.ui.prefs.CVBillModePreference.java
de.ub0r.android.callmeter.ui.prefs.CVCheckBoxPreference.java
de.ub0r.android.callmeter.ui.prefs.CVDatePreference.java
de.ub0r.android.callmeter.ui.prefs.CVEditTextPreference.java
de.ub0r.android.callmeter.ui.prefs.CVListPreference.java
de.ub0r.android.callmeter.ui.prefs.HourGroupEdit.java
de.ub0r.android.callmeter.ui.prefs.HourGroups.java
de.ub0r.android.callmeter.ui.prefs.NumberGroupEdit.java
de.ub0r.android.callmeter.ui.prefs.NumberGroups.java
de.ub0r.android.callmeter.ui.prefs.PlanEdit.java
de.ub0r.android.callmeter.ui.prefs.Plans.java
de.ub0r.android.callmeter.ui.prefs.PreferencesImport.java
de.ub0r.android.callmeter.ui.prefs.PreferencesPlain.java
de.ub0r.android.callmeter.ui.prefs.PreferencesRules.java
de.ub0r.android.callmeter.ui.prefs.Preferences.java
de.ub0r.android.callmeter.ui.prefs.RuleEdit.java
de.ub0r.android.callmeter.ui.prefs.Rules.java
de.ub0r.android.callmeter.ui.prefs.SimplePreferencesChild.java
de.ub0r.android.callmeter.ui.prefs.SimplePreferences.java
de.ub0r.android.callmeter.ui.prefs.UpDownPreference.java
de.ub0r.android.callmeter.ui.prefs.UpdateListener.java
de.ub0r.android.callmeter.widget.LogsAppWidgetConfigure.java
de.ub0r.android.callmeter.widget.LogsAppWidgetProvider.java
de.ub0r.android.callmeter.widget.StatsAppWidgetConfigure.java
de.ub0r.android.callmeter.widget.StatsAppWidgetProvider.java
yuku.ambilwarna.AmbilWarnaDialog.java
yuku.ambilwarna.AmbilWarnaKotak.java