diff options
Diffstat (limited to 'src/com/android/settings/cmstats/ReportingService.java')
| -rw-r--r-- | src/com/android/settings/cmstats/ReportingService.java | 208 |
1 files changed, 85 insertions, 123 deletions
diff --git a/src/com/android/settings/cmstats/ReportingService.java b/src/com/android/settings/cmstats/ReportingService.java index 9087fd7f4..616ac9ba1 100644 --- a/src/com/android/settings/cmstats/ReportingService.java +++ b/src/com/android/settings/cmstats/ReportingService.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2012 The CyanogenMod Project + * Copyright (C) 2015 The CyanogenMod Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,143 +16,105 @@ package com.android.settings.cmstats; -import android.app.Service; +import android.app.IntentService; +import android.app.job.JobInfo; +import android.app.job.JobScheduler; +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.os.PersistableBundle; import android.util.Log; -import com.android.settings.R; -import com.android.settings.Settings; - -import com.google.analytics.tracking.android.GoogleAnalytics; -import com.google.analytics.tracking.android.Tracker; -import com.google.analytics.tracking.android.MapBuilder; - -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; -import java.util.Map; -public class ReportingService extends Service { +public class ReportingService extends IntentService { /* package */ static final String TAG = "CMStats"; + private static final boolean DEBUG = false; - private StatsUploadTask mTask; - - @Override - public IBinder onBind(Intent intent) { - return null; + public ReportingService() { + super(ReportingService.class.getSimpleName()); } @Override - public int onStartCommand (Intent intent, int flags, int startId) { - Log.d(TAG, "User has opted in -- reporting."); - - if (mTask == null || mTask.getStatus() == AsyncTask.Status.FINISHED) { - mTask = new StatsUploadTask(); - mTask.execute(); - } - - return Service.START_REDELIVER_INTENT; - } - - 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 - Tracker tracker = GoogleAnalytics.getInstance(ReportingService.this) - .getTracker(getString(R.string.ga_trackingId)); - tracker.send(createMap(deviceName, deviceVersion,deviceCountry)); - - // 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.send(createMap("checkin", deviceName, deviceVersionNoDevice)); - } - - // report to the cmstats service - HttpClient httpClient = new DefaultHttpClient(); - HttpPost httpPost = new HttpPost("https://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; - } - - @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; + protected void onHandleIntent(Intent intent) { + JobScheduler js = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE); + + if (AnonymousStats.getNextJobId(this) == -1) { + // if we've filled up to the threshold, we may have some stale job queue ids, purge them + // then re-add what hasn't executed yet + AnonymousStats.clearJobQueue(this); + + final List<JobInfo> allPendingJobs = js.getAllPendingJobs(); + + // add two extra jobs to the size for what we will schedule below so we *always* + // have room for both. + if (js.getAllPendingJobs().size() + 2 >= AnonymousStats.QUEUE_MAX_THRESHOLD) { + // there are still as many actual pending jobs as our threshold allows. + // since we are past the threshold we will be losing data if we don't schedule + // another job here, so just clear out all the old data and start fresh + js.cancelAll(); } else { - // error, try again in 3 hours - interval = 3L * 60L * 60L * 1000L; + for (JobInfo pendingJob : allPendingJobs) { + AnonymousStats.addJob(this, pendingJob.getId()); + } } - - ReportingServiceManager.setAlarm(context, interval); - stopSelf(); } - } - private Map<String, String> createMap(String category, String action, String label) { - return MapBuilder.createEvent(category, // Event category (required) - action, // Event action (required) - label, // Event label - null) // Event value - .build(); + int cyanogenJobId, cmOrgJobId; + AnonymousStats.addJob(this, cyanogenJobId = AnonymousStats.getNextJobId(this)); + AnonymousStats.addJob(this, cmOrgJobId = AnonymousStats.getNextJobId(this)); + + if (DEBUG) Log.d(TAG, "scheduling jobs id: " + cyanogenJobId + ", " + cmOrgJobId); + + // get snapshot and persist it + 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()); + + PersistableBundle cyanogenBundle = new PersistableBundle(); + cyanogenBundle.putString(StatsUploadJobService.KEY_DEVICE_NAME, deviceId); + cyanogenBundle.putString(StatsUploadJobService.KEY_UNIQUE_ID, deviceName); + cyanogenBundle.putString(StatsUploadJobService.KEY_VERSION, deviceVersion); + cyanogenBundle.putString(StatsUploadJobService.KEY_COUNTRY, deviceCountry); + cyanogenBundle.putString(StatsUploadJobService.KEY_CARRIER, deviceCarrier); + cyanogenBundle.putString(StatsUploadJobService.KEY_CARRIER_ID, deviceCarrierId); + cyanogenBundle.putLong(StatsUploadJobService.KEY_TIMESTAMP, System.currentTimeMillis()); + + PersistableBundle cmBundle = new PersistableBundle(cyanogenBundle); + + // set job types + cyanogenBundle.putInt(StatsUploadJobService.KEY_JOB_TYPE, + StatsUploadJobService.JOB_TYPE_CYANOGEN); + cmBundle.putInt(StatsUploadJobService.KEY_JOB_TYPE, + StatsUploadJobService.JOB_TYPE_CMORG); + + // schedule cyanogen stats upload + js.schedule(new JobInfo.Builder(cyanogenJobId, new ComponentName(getPackageName(), + StatsUploadJobService.class.getName())) + .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY) + .setMinimumLatency(1000) + .setExtras(cyanogenBundle) + .setPersisted(true) + .build()); + + // schedule cmorg stats upload + js.schedule(new JobInfo.Builder(cmOrgJobId, new ComponentName(getPackageName(), + StatsUploadJobService.class.getName())) + .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY) + .setMinimumLatency(1000) + .setExtras(cmBundle) + .setPersisted(true) + .build()); + + // reschedule + final SharedPreferences prefs = AnonymousStats.getPreferences(this); + prefs.edit().putLong(AnonymousStats.ANONYMOUS_LAST_CHECKED, + System.currentTimeMillis()).apply(); + ReportingServiceManager.setAlarm(this, 0); } + } |
