diff options
author | Danny Baumann <dannybaumann@web.de> | 2013-05-27 23:40:31 -0700 |
---|---|---|
committer | Gerrit Code Review <gerrit@cyanogenmod.org> | 2013-05-27 23:40:31 -0700 |
commit | 0385685271b227ca750a7160162b279ea2887969 (patch) | |
tree | 1591e3f53cc090aba82d1b67f6e2de022cfafb61 | |
parent | 2cd49f09370c1a6803380fe982f66a0915b2bef7 (diff) | |
parent | 636dce03ed59ed88d6940648f5fb99b49b209360 (diff) | |
download | packages_apps_settings-0385685271b227ca750a7160162b279ea2887969.zip packages_apps_settings-0385685271b227ca750a7160162b279ea2887969.tar.gz packages_apps_settings-0385685271b227ca750a7160162b279ea2887969.tar.bz2 |
Merge "Clean up CMStats code." into cm-10.1
5 files changed, 191 insertions, 185 deletions
diff --git a/src/com/android/settings/cmstats/AnonymousStats.java b/src/com/android/settings/cmstats/AnonymousStats.java index e2334c1..66c23ef 100644 --- a/src/com/android/settings/cmstats/AnonymousStats.java +++ b/src/com/android/settings/cmstats/AnonymousStats.java @@ -18,7 +18,6 @@ package com.android.settings.cmstats; import android.app.AlertDialog; import android.app.Dialog; -import android.app.NotificationManager; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; @@ -32,36 +31,38 @@ import android.preference.PreferenceScreen; import com.android.settings.R; import com.android.settings.SettingsPreferenceFragment; -public class AnonymousStats extends SettingsPreferenceFragment - implements DialogInterface.OnClickListener, DialogInterface.OnDismissListener, +public class AnonymousStats extends SettingsPreferenceFragment implements + DialogInterface.OnClickListener, DialogInterface.OnDismissListener, Preference.OnPreferenceChangeListener { - private static final String VIEW_STATS = "pref_view_stats"; - protected static final String ANONYMOUS_OPT_IN = "pref_anonymous_opt_in"; - - protected static final String ANONYMOUS_LAST_CHECKED = "pref_anonymous_checked_in"; + private static final String PREF_FILE_NAME = "CMStats"; + /* package */ static final String ANONYMOUS_OPT_IN = "pref_anonymous_opt_in"; + /* package */ static final String ANONYMOUS_LAST_CHECKED = "pref_anonymous_checked_in"; private CheckBoxPreference mEnableReporting; - private Preference mViewStats; private Dialog mOkDialog; - private boolean mOkClicked; private SharedPreferences mPrefs; + public static SharedPreferences getPreferences(Context context) { + return context.getSharedPreferences(PREF_FILE_NAME, 0); + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - if (getPreferenceManager() != null) { - addPreferencesFromResource(R.xml.anonymous_stats); - PreferenceScreen prefSet = getPreferenceScreen(); - mPrefs = getActivity().getSharedPreferences("CMStats", 0); - mEnableReporting = (CheckBoxPreference) prefSet.findPreference(ANONYMOUS_OPT_IN); - mViewStats = (Preference) prefSet.findPreference(VIEW_STATS); - } + + addPreferencesFromResource(R.xml.anonymous_stats); + + mPrefs = getPreferences(getActivity()); + + PreferenceScreen prefSet = getPreferenceScreen(); + mEnableReporting = (CheckBoxPreference) prefSet.findPreference(ANONYMOUS_OPT_IN); + mViewStats = (Preference) prefSet.findPreference(VIEW_STATS); } @Override @@ -72,14 +73,13 @@ public class AnonymousStats extends SettingsPreferenceFragment mOkClicked = false; if (mOkDialog != null) { mOkDialog.dismiss(); - mOkDialog = null; } - mOkDialog = new AlertDialog.Builder(getActivity()).setMessage( - getActivity().getResources().getString(R.string.anonymous_statistics_warning)) + mOkDialog = new AlertDialog.Builder(getActivity()) + .setMessage(R.string.anonymous_statistics_warning) .setTitle(R.string.anonymous_statistics_warning_title) .setIconAttribute(android.R.attr.alertDialogIcon) .setPositiveButton(android.R.string.yes, this) - .setNeutralButton(getString(R.string.anonymous_learn_more), this) + .setNeutralButton(R.string.anonymous_learn_more, this) .setNegativeButton(android.R.string.no, this) .show(); mOkDialog.setOnDismissListener(this); @@ -116,12 +116,11 @@ public class AnonymousStats extends SettingsPreferenceFragment mOkClicked = true; mPrefs.edit().putBoolean(ANONYMOUS_OPT_IN, true).apply(); ReportingServiceManager.launchService(getActivity()); - } else if (which == DialogInterface.BUTTON_NEGATIVE){ + } else if (which == DialogInterface.BUTTON_NEGATIVE) { mEnableReporting.setChecked(false); } else { Uri uri = Uri.parse("http://www.cyanogenmod.org/blog/cmstats-what-it-is-and-why-you-should-opt-in"); startActivity(new Intent(Intent.ACTION_VIEW, uri)); } } - } diff --git a/src/com/android/settings/cmstats/PreviewData.java b/src/com/android/settings/cmstats/PreviewData.java index c8b18ba..7a78aea 100644 --- a/src/com/android/settings/cmstats/PreviewData.java +++ b/src/com/android/settings/cmstats/PreviewData.java @@ -16,6 +16,7 @@ package com.android.settings.cmstats; +import android.content.Context; import android.os.Bundle; import android.preference.Preference; import android.preference.PreferenceScreen; @@ -23,54 +24,26 @@ import android.preference.PreferenceScreen; import com.android.settings.R; import com.android.settings.SettingsPreferenceFragment; -public class PreviewData extends SettingsPreferenceFragment - implements Preference.OnPreferenceChangeListener{ - +public class PreviewData extends SettingsPreferenceFragment { private static final String UNIQUE_ID = "preview_id"; - private static final String DEVICE = "preview_device"; - private static final String VERSION = "preview_version"; - private static final String COUNTRY = "preview_country"; - private static final String CARRIER = "preview_carrier"; - private Preference mId; - - private Preference mDevice; - - private Preference mVersion; - - private Preference mCountry; - - private Preference mCarrier; - @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - if (getPreferenceManager() != null) { - - addPreferencesFromResource(R.xml.preview_data); - PreferenceScreen prefSet = getPreferenceScreen(); + addPreferencesFromResource(R.xml.preview_data); - mId = (Preference) prefSet.findPreference(UNIQUE_ID); - mDevice = (Preference) prefSet.findPreference(DEVICE); - mVersion = (Preference) prefSet.findPreference(VERSION); - mCountry = (Preference) prefSet.findPreference(COUNTRY); - mCarrier = (Preference) prefSet.findPreference(CARRIER); + final PreferenceScreen prefSet = getPreferenceScreen(); + final Context context = getActivity(); - mId.setSummary(Utilities.getUniqueID(getActivity().getApplicationContext())); - mDevice.setSummary(Utilities.getDevice()); - mVersion.setSummary(Utilities.getModVersion()); - mCountry.setSummary(Utilities.getCountryCode(getActivity().getApplicationContext())); - mCarrier.setSummary(Utilities.getCarrier(getActivity().getApplicationContext())); - } - } - - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - return false; + prefSet.findPreference(UNIQUE_ID).setSummary(Utilities.getUniqueID(context)); + prefSet.findPreference(DEVICE).setSummary(Utilities.getDevice()); + prefSet.findPreference(VERSION).setSummary(Utilities.getModVersion()); + prefSet.findPreference(COUNTRY).setSummary(Utilities.getCountryCode(context)); + prefSet.findPreference(CARRIER).setSummary(Utilities.getCarrier(context)); } } diff --git a/src/com/android/settings/cmstats/ReportingService.java b/src/com/android/settings/cmstats/ReportingService.java index b8b54d2..c76dc04 100644 --- a/src/com/android/settings/cmstats/ReportingService.java +++ b/src/com/android/settings/cmstats/ReportingService.java @@ -16,23 +16,11 @@ package com.android.settings.cmstats; -import java.util.ArrayList; -import java.util.List; - -import org.apache.http.NameValuePair; -import org.apache.http.client.HttpClient; -import org.apache.http.client.entity.UrlEncodedFormEntity; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.impl.client.DefaultHttpClient; -import org.apache.http.message.BasicNameValuePair; - -import android.app.Notification; -import android.app.NotificationManager; -import android.app.PendingIntent; import android.app.Service; -import android.content.ComponentName; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; +import android.os.AsyncTask; import android.os.IBinder; import android.util.Log; @@ -42,10 +30,21 @@ import com.android.settings.Settings; import com.google.analytics.tracking.android.GoogleAnalytics; import com.google.analytics.tracking.android.Tracker; -import com.android.settings.R; +import org.apache.http.NameValuePair; +import org.apache.http.client.HttpClient; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.message.BasicNameValuePair; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; public class ReportingService extends Service { - protected static final String TAG = "CMStats"; + /* package */ static final String TAG = "CMStats"; + + private StatsUploadTask mTask; @Override public IBinder onBind(Intent intent) { @@ -55,69 +54,96 @@ public class ReportingService extends Service { @Override public int onStartCommand (Intent intent, int flags, int startId) { Log.d(TAG, "User has opted in -- reporting."); - Thread thread = new Thread() { - @Override - public void run() { - report(); - } - }; - thread.start(); + + if (mTask == null || mTask.getStatus() == AsyncTask.Status.FINISHED) { + mTask = new StatsUploadTask(); + mTask.execute(); + } + return Service.START_REDELIVER_INTENT; } - private void report() { - String deviceId = Utilities.getUniqueID(getApplicationContext()); - String deviceName = Utilities.getDevice(); - String deviceVersion = Utilities.getModVersion(); - String deviceCountry = Utilities.getCountryCode(getApplicationContext()); - String deviceCarrier = Utilities.getCarrier(getApplicationContext()); - String deviceCarrierId = Utilities.getCarrierId(getApplicationContext()); - - Log.d(TAG, "SERVICE: Device ID=" + deviceId); - Log.d(TAG, "SERVICE: Device Name=" + deviceName); - Log.d(TAG, "SERVICE: Device Version=" + deviceVersion); - Log.d(TAG, "SERVICE: Country=" + deviceCountry); - Log.d(TAG, "SERVICE: Carrier=" + deviceCarrier); - Log.d(TAG, "SERVICE: Carrier ID=" + deviceCarrierId); - - // report to google analytics - GoogleAnalytics ga = GoogleAnalytics.getInstance(this); - Tracker tracker = ga.getTracker(getString(R.string.ga_trackingId)); - tracker.sendEvent(deviceName, deviceVersion, deviceCountry, null); - // this really should be set at build time... - // format of version should be: - // version[-date-type]-device - String[] parts = deviceVersion.split("-"); - String deviceVersionNoDevice = null; - if (parts.length == 2) { - deviceVersionNoDevice = parts[0]; - } - else if (parts.length == 4) { - deviceVersionNoDevice = parts[0] + "-" + parts[2]; + private class StatsUploadTask extends AsyncTask<Void, Void, Boolean> { + @Override + protected Boolean doInBackground(Void... params) { + String deviceId = Utilities.getUniqueID(getApplicationContext()); + String deviceName = Utilities.getDevice(); + String deviceVersion = Utilities.getModVersion(); + String deviceCountry = Utilities.getCountryCode(getApplicationContext()); + String deviceCarrier = Utilities.getCarrier(getApplicationContext()); + String deviceCarrierId = Utilities.getCarrierId(getApplicationContext()); + + Log.d(TAG, "SERVICE: Device ID=" + deviceId); + Log.d(TAG, "SERVICE: Device Name=" + deviceName); + Log.d(TAG, "SERVICE: Device Version=" + deviceVersion); + Log.d(TAG, "SERVICE: Country=" + deviceCountry); + Log.d(TAG, "SERVICE: Carrier=" + deviceCarrier); + Log.d(TAG, "SERVICE: Carrier ID=" + deviceCarrierId); + + // report to google analytics + GoogleAnalytics ga = GoogleAnalytics.getInstance(ReportingService.this); + Tracker tracker = ga.getTracker(getString(R.string.ga_trackingId)); + tracker.sendEvent(deviceName, deviceVersion, deviceCountry, null); + + // this really should be set at build time... + // format of version should be: + // version[-date-type]-device + String[] parts = deviceVersion.split("-"); + String deviceVersionNoDevice = null; + if (parts.length == 2) { + deviceVersionNoDevice = parts[0]; + } else if (parts.length == 4) { + deviceVersionNoDevice = parts[0] + "-" + parts[2]; + } + + if (deviceVersionNoDevice != null) { + tracker.sendEvent("checkin", deviceName, deviceVersionNoDevice, null); + } + tracker.close(); + + // report to the cmstats service + HttpClient httpClient = new DefaultHttpClient(); + HttpPost httpPost = new HttpPost("http://stats.cyanogenmod.org/submit"); + boolean success = false; + + try { + List<NameValuePair> kv = new ArrayList<NameValuePair>(5); + kv.add(new BasicNameValuePair("device_hash", deviceId)); + kv.add(new BasicNameValuePair("device_name", deviceName)); + kv.add(new BasicNameValuePair("device_version", deviceVersion)); + kv.add(new BasicNameValuePair("device_country", deviceCountry)); + kv.add(new BasicNameValuePair("device_carrier", deviceCarrier)); + kv.add(new BasicNameValuePair("device_carrier_id", deviceCarrierId)); + + httpPost.setEntity(new UrlEncodedFormEntity(kv)); + httpClient.execute(httpPost); + + success = true; + } catch (IOException e) { + Log.w(TAG, "Could not upload stats checkin", e); + } + + return success; } - if (deviceVersionNoDevice != null) - tracker.sendEvent("checkin", deviceName, deviceVersionNoDevice, null); - tracker.close(); - - // report to the cmstats service - HttpClient httpclient = new DefaultHttpClient(); - HttpPost httppost = new HttpPost("http://stats.cyanogenmod.org/submit"); - try { - List<NameValuePair> kv = new ArrayList<NameValuePair>(5); - kv.add(new BasicNameValuePair("device_hash", deviceId)); - kv.add(new BasicNameValuePair("device_name", deviceName)); - kv.add(new BasicNameValuePair("device_version", deviceVersion)); - kv.add(new BasicNameValuePair("device_country", deviceCountry)); - kv.add(new BasicNameValuePair("device_carrier", deviceCarrier)); - kv.add(new BasicNameValuePair("device_carrier_id", deviceCarrierId)); - httppost.setEntity(new UrlEncodedFormEntity(kv)); - httpclient.execute(httppost); - getSharedPreferences("CMStats", 0).edit().putLong(AnonymousStats.ANONYMOUS_LAST_CHECKED, - System.currentTimeMillis()).apply(); - } catch (Exception e) { - Log.e(TAG, "Got Exception", e); + + @Override + protected void onPostExecute(Boolean result) { + final Context context = ReportingService.this; + long interval; + + if (result) { + final SharedPreferences prefs = AnonymousStats.getPreferences(context); + prefs.edit().putLong(AnonymousStats.ANONYMOUS_LAST_CHECKED, + System.currentTimeMillis()).apply(); + // use set interval + interval = 0; + } else { + // error, try again in 3 hours + interval = 3L * 60L * 60L * 1000L; + } + + ReportingServiceManager.setAlarm(context, interval); + stopSelf(); } - ReportingServiceManager.setAlarm(this); - stopSelf(); } } diff --git a/src/com/android/settings/cmstats/ReportingServiceManager.java b/src/com/android/settings/cmstats/ReportingServiceManager.java index f6ef912..354d0d3 100644 --- a/src/com/android/settings/cmstats/ReportingServiceManager.java +++ b/src/com/android/settings/cmstats/ReportingServiceManager.java @@ -28,65 +28,75 @@ import android.net.NetworkInfo; import android.util.Log; public class ReportingServiceManager extends BroadcastReceiver { - - public static final long dMill = 24L * 60L * 60L * 1000L; - public static final long tFrame = 1L * dMill; + private static final long MILLIS_PER_HOUR = 60L * 60L * 1000L; + private static final long MILLIS_PER_DAY = 24L * MILLIS_PER_HOUR; + private static final long UPDATE_INTERVAL = 1L * MILLIS_PER_DAY; @Override - public void onReceive(Context ctx, Intent intent) { + public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { - setAlarm(ctx); + setAlarm(context, 0); } else { - launchService(ctx); + launchService(context); } } - protected static void setAlarm (Context ctx) { - SharedPreferences prefs = ctx.getSharedPreferences("CMStats", 0); + public static void setAlarm(Context context, long millisFromNow) { + SharedPreferences prefs = AnonymousStats.getPreferences(context); boolean optedIn = prefs.getBoolean(AnonymousStats.ANONYMOUS_OPT_IN, true); if (!optedIn) { return; } - long lastSynced = prefs.getLong(AnonymousStats.ANONYMOUS_LAST_CHECKED, 0); - if (lastSynced == 0) { - // never synced, so let's fake out that the last sync was just now. - // this will allow the user tFrame time to opt out before it will start - // sending up anonymous stats. - lastSynced = System.currentTimeMillis(); - prefs.edit().putLong(AnonymousStats.ANONYMOUS_LAST_CHECKED, lastSynced).apply(); - Log.d(ReportingService.TAG, "Set alarm for first sync."); + + if (millisFromNow <= 0) { + long lastSynced = prefs.getLong(AnonymousStats.ANONYMOUS_LAST_CHECKED, 0); + if (lastSynced == 0) { + // never synced, so let's fake out that the last sync was just now. + // this will allow the user tFrame time to opt out before it will start + // sending up anonymous stats. + lastSynced = System.currentTimeMillis(); + prefs.edit().putLong(AnonymousStats.ANONYMOUS_LAST_CHECKED, lastSynced).apply(); + Log.d(ReportingService.TAG, "Set alarm for first sync."); + } + millisFromNow = (lastSynced + UPDATE_INTERVAL) - System.currentTimeMillis(); } - long timeLeft = (lastSynced + tFrame) - System.currentTimeMillis(); - Intent sIntent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION); - sIntent.setComponent(new ComponentName(ctx.getPackageName(), ReportingServiceManager.class.getName())); - AlarmManager alarmManager = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE); - alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + timeLeft, PendingIntent.getBroadcast(ctx, 0, sIntent, 0)); - Log.d(ReportingService.TAG, "Next sync attempt in : " + timeLeft * 24 / dMill + " hours"); + + Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION); + intent.setClass(context, ReportingServiceManager.class); + + AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); + alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + millisFromNow, + PendingIntent.getBroadcast(context, 0, intent, 0)); + Log.d(ReportingService.TAG, "Next sync attempt in : " + millisFromNow / MILLIS_PER_HOUR + " hours"); } - public static void launchService (Context ctx) { - ConnectivityManager cm = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE); + public static void launchService(Context context) { + ConnectivityManager cm = (ConnectivityManager) + context.getSystemService(Context.CONNECTIVITY_SERVICE); + NetworkInfo networkInfo = cm.getActiveNetworkInfo(); if (networkInfo == null || !networkInfo.isConnected()) { return; } - SharedPreferences prefs = ctx.getSharedPreferences("CMStats", 0); + + SharedPreferences prefs = AnonymousStats.getPreferences(context); boolean optedIn = prefs.getBoolean(AnonymousStats.ANONYMOUS_OPT_IN, true); if (!optedIn) { return; } long lastSynced = prefs.getLong(AnonymousStats.ANONYMOUS_LAST_CHECKED, 0); if (lastSynced == 0) { - setAlarm(ctx); + setAlarm(context, 0); return; } long timeLeft = System.currentTimeMillis() - lastSynced; - if (timeLeft < tFrame) { - Log.d(ReportingService.TAG, "Waiting for next sync : " + timeLeft * 24 / dMill + " hours"); + if (timeLeft < UPDATE_INTERVAL) { + Log.d(ReportingService.TAG, "Waiting for next sync : " + timeLeft / MILLIS_PER_HOUR + " hours"); return; } - Intent sIntent = new Intent(); - sIntent.setComponent(new ComponentName(ctx.getPackageName(), ReportingService.class.getName())); - ctx.startService(sIntent); + + Intent intent = new Intent(); + intent.setClass(context, ReportingService.class); + context.startService(intent); } } diff --git a/src/com/android/settings/cmstats/Utilities.java b/src/com/android/settings/cmstats/Utilities.java index 4548ded..496f398 100644 --- a/src/com/android/settings/cmstats/Utilities.java +++ b/src/com/android/settings/cmstats/Utilities.java @@ -16,45 +16,44 @@ package com.android.settings.cmstats; -import java.math.BigInteger; -import java.net.NetworkInterface; -import java.security.MessageDigest; - import android.content.Context; import android.os.SystemProperties; import android.provider.Settings; import android.telephony.TelephonyManager; +import android.text.TextUtils; + +import java.math.BigInteger; +import java.net.NetworkInterface; +import java.security.MessageDigest; public class Utilities { - public static String getUniqueID(Context ctx) { - return digest(ctx.getPackageName() + Settings.Secure.getString(ctx.getContentResolver(), Settings.Secure.ANDROID_ID)); + public static String getUniqueID(Context context) { + final String id = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID); + return digest(context.getPackageName() + id); } - public static String getCarrier(Context ctx) { - TelephonyManager tm = (TelephonyManager) ctx - .getSystemService(Context.TELEPHONY_SERVICE); + public static String getCarrier(Context context) { + TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); String carrier = tm.getNetworkOperatorName(); - if ("".equals(carrier)) { + if (TextUtils.isEmpty(carrier)) { carrier = "Unknown"; } return carrier; } - public static String getCarrierId(Context ctx) { - TelephonyManager tm = (TelephonyManager) ctx - .getSystemService(Context.TELEPHONY_SERVICE); + public static String getCarrierId(Context context) { + TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); String carrierId = tm.getNetworkOperator(); - if ("".equals(carrierId)) { + if (TextUtils.isEmpty(carrierId)) { carrierId = "0"; } return carrierId; } - public static String getCountryCode(Context ctx) { - TelephonyManager tm = (TelephonyManager) ctx - .getSystemService(Context.TELEPHONY_SERVICE); + public static String getCountryCode(Context context) { + TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); String countryCode = tm.getNetworkCountryIso(); - if (countryCode.equals("")) { + if (TextUtils.isEmpty(countryCode)) { countryCode = "Unknown"; } return countryCode; @@ -71,8 +70,7 @@ public class Utilities { public static String digest(String input) { try { MessageDigest md = MessageDigest.getInstance("MD5"); - return new BigInteger(1, md.digest(input.getBytes())).toString(16) - .toUpperCase(); + return new BigInteger(1, md.digest(input.getBytes())).toString(16).toUpperCase(); } catch (Exception e) { return null; } |