Merge "b/16804395. Migrate QuickResponses from Telephony as needed." into lmp-dev
diff --git a/src/com/android/telecomm/QuickResponseUtils.java b/src/com/android/telecomm/QuickResponseUtils.java
new file mode 100644
index 0000000..ead9105
--- /dev/null
+++ b/src/com/android/telecomm/QuickResponseUtils.java
@@ -0,0 +1,116 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * 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.android.telecomm;
+
+import android.content.Context;
+import android.content.SharedPreferences;
+import android.content.pm.PackageManager;
+import android.content.res.Resources;
+
+
+/**
+ * Utils class that exposes some helper routines to used to manage the QuickResponses
+ */
+public class QuickResponseUtils {
+ public static final String LOG_TAG = "QuickResponseUtils";
+
+ // SharedPreferences file name for our persistent settings.
+ public static final String SHARED_PREFERENCES_NAME = "respond_via_sms_prefs";
+ private static final String PACKAGE_NAME_TELEPHONY = "com.android.phone";
+
+ // Preference keys for the 4 "canned responses"; see RespondViaSmsManager$Settings.
+ // Since (for now at least) the number of messages is fixed at 4, and since
+ // SharedPreferences can't deal with arrays anyway, just store the messages
+ // as 4 separate strings.
+ public static final int NUM_CANNED_RESPONSES = 4;
+ public static final String KEY_CANNED_RESPONSE_PREF_1 = "canned_response_pref_1";
+ public static final String KEY_CANNED_RESPONSE_PREF_2 = "canned_response_pref_2";
+ public static final String KEY_CANNED_RESPONSE_PREF_3 = "canned_response_pref_3";
+ public static final String KEY_CANNED_RESPONSE_PREF_4 = "canned_response_pref_4";
+
+ /**
+ * As of L, QuickResponses were moved from Telephony to Telecomm. Because of
+ * this, we need to make sure that we migrate any old QuickResponses to our
+ * current SharedPreferences. This is a lazy migration as it happens only when
+ * the QuickResponse settings are viewed or if they are queried via RespondViaSmsManager.
+ */
+ public static void maybeMigrateLegacyQuickResponses() {
+ // The algorithm will go as such:
+ // If Telecomm QuickResponses exist, we will skip migration because this implies
+ // that a user has already specified their desired QuickResponses and have abandoned any
+ // older QuickResponses.
+ // Then, if Telephony QuickResponses exist, we will move those to Telecomm.
+ // If neither exist, we'll populate Telecomm with the default QuickResponses.
+ // This guarantees the caller that QuickResponses exist in SharedPreferences after this
+ // function is called.
+
+ Log.d(LOG_TAG, "maybeMigrateLegacyQuickResponses() - Starting");
+
+ final Context telecommContext = TelecommApp.getInstance();
+ final SharedPreferences prefs = telecommContext.getSharedPreferences(
+ SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
+ final Resources res = telecommContext.getResources();
+
+ final boolean responsesExist = prefs.contains(KEY_CANNED_RESPONSE_PREF_1);
+ if (responsesExist) {
+ // If one QuickResponse exists, they all exist.
+ Log.d(LOG_TAG, "maybeMigrateLegacyQuickResponses() - Telecomm QuickResponses exist");
+ return;
+ }
+
+ // Grab the all the default QuickResponses from our resources.
+ String cannedResponse1 = res.getString(R.string.respond_via_sms_canned_response_1);
+ String cannedResponse2 = res.getString(R.string.respond_via_sms_canned_response_2);
+ String cannedResponse3 = res.getString(R.string.respond_via_sms_canned_response_3);
+ String cannedResponse4 = res.getString(R.string.respond_via_sms_canned_response_4);
+
+ Log.d(LOG_TAG, "maybeMigrateLegacyQuickResponses() - No local QuickResponses");
+
+ // We don't have local QuickResponses, let's see if they live in
+ // the Telephony package and we'll fall back on using our default values.
+ Context telephonyContext = null;
+ try {
+ telephonyContext = telecommContext.createPackageContext(PACKAGE_NAME_TELEPHONY, 0);
+ } catch (PackageManager.NameNotFoundException e) {
+ Log.e(LOG_TAG, e, "maybeMigrateLegacyQuickResponses() - Can't find Telephony package.");
+ }
+
+ // Read the old canned responses from the Telephony SharedPreference if possible.
+ if (telephonyContext != null) {
+ // Note that if any one QuickResponse does not exist, we'll use the default
+ // value to populate it.
+ Log.d(LOG_TAG, "maybeMigrateLegacyQuickResponses() - Using Telephony QuickResponses.");
+ final SharedPreferences oldPrefs = telephonyContext.getSharedPreferences(
+ SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
+ cannedResponse1 = oldPrefs.getString(KEY_CANNED_RESPONSE_PREF_1, cannedResponse1);
+ cannedResponse2 = oldPrefs.getString(KEY_CANNED_RESPONSE_PREF_2, cannedResponse2);
+ cannedResponse3 = oldPrefs.getString(KEY_CANNED_RESPONSE_PREF_3, cannedResponse3);
+ cannedResponse4 = oldPrefs.getString(KEY_CANNED_RESPONSE_PREF_4, cannedResponse4);
+ }
+
+ // Either way, write them back into Telecomm SharedPreferences.
+ final SharedPreferences.Editor editor = prefs.edit();
+ editor.putString(KEY_CANNED_RESPONSE_PREF_1, cannedResponse1);
+ editor.putString(KEY_CANNED_RESPONSE_PREF_2, cannedResponse2);
+ editor.putString(KEY_CANNED_RESPONSE_PREF_3, cannedResponse3);
+ editor.putString(KEY_CANNED_RESPONSE_PREF_4, cannedResponse4);
+ editor.commit();
+
+ Log.d(LOG_TAG, "maybeMigrateLegacyQuickResponses() - Done.");
+ return;
+ }
+}
diff --git a/src/com/android/telecomm/RespondViaSmsManager.java b/src/com/android/telecomm/RespondViaSmsManager.java
index 8618af0..f1e6033 100644
--- a/src/com/android/telecomm/RespondViaSmsManager.java
+++ b/src/com/android/telecomm/RespondViaSmsManager.java
@@ -40,19 +40,6 @@
public class RespondViaSmsManager extends CallsManagerListenerBase {
private static final String SCHEME_SMSTO = "smsto";
- /** SharedPreferences file name for our persistent settings. */
- private static final String SHARED_PREFERENCES_NAME = "respond_via_sms_prefs";
-
- // Preference keys for the 4 "canned responses"; see RespondViaSmsManager$Settings.
- // Since (for now at least) the number of messages is fixed at 4, and since
- // SharedPreferences can't deal with arrays anyway, just store the messages
- // as 4 separate strings.
- private static final int NUM_CANNED_RESPONSES = 4;
- private static final String KEY_CANNED_RESPONSE_PREF_1 = "canned_response_pref_1";
- private static final String KEY_CANNED_RESPONSE_PREF_2 = "canned_response_pref_2";
- private static final String KEY_CANNED_RESPONSE_PREF_3 = "canned_response_pref_3";
- private static final String KEY_CANNED_RESPONSE_PREF_4 = "canned_response_pref_4";
-
private static final int MSG_CANNED_TEXT_MESSAGES_READY = 1;
private static final int MSG_SHOW_SENT_TOAST = 2;
@@ -104,22 +91,28 @@
@Override
public void run() {
Log.d(RespondViaSmsManager.this, "loadCannedResponses() starting");
+
+ // This function guarantees that QuickResponses will be in our
+ // SharedPreferences with the proper values considering there may be
+ // old QuickResponses in Telephony pre L.
+ QuickResponseUtils.maybeMigrateLegacyQuickResponses();
+
final SharedPreferences prefs = TelecommApp.getInstance().getSharedPreferences(
- SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
+ QuickResponseUtils.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
final Resources res = TelecommApp.getInstance().getInstance().getResources();
- final ArrayList<String> textMessages = new ArrayList<>(NUM_CANNED_RESPONSES);
+ final ArrayList<String> textMessages = new ArrayList<>(
+ QuickResponseUtils.NUM_CANNED_RESPONSES);
// Note the default values here must agree with the corresponding
// android:defaultValue attributes in respond_via_sms_settings.xml.
-
- textMessages.add(0, prefs.getString(KEY_CANNED_RESPONSE_PREF_1,
+ textMessages.add(0, prefs.getString(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_1,
res.getString(R.string.respond_via_sms_canned_response_1)));
- textMessages.add(1, prefs.getString(KEY_CANNED_RESPONSE_PREF_2,
+ textMessages.add(1, prefs.getString(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_2,
res.getString(R.string.respond_via_sms_canned_response_2)));
- textMessages.add(2, prefs.getString(KEY_CANNED_RESPONSE_PREF_3,
+ textMessages.add(2, prefs.getString(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_3,
res.getString(R.string.respond_via_sms_canned_response_3)));
- textMessages.add(3, prefs.getString(KEY_CANNED_RESPONSE_PREF_4,
+ textMessages.add(3, prefs.getString(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_4,
res.getString(R.string.respond_via_sms_canned_response_4)));
Log.d(RespondViaSmsManager.this,
diff --git a/src/com/android/telecomm/RespondViaSmsSettings.java b/src/com/android/telecomm/RespondViaSmsSettings.java
index 231dbaa..ecca39c 100644
--- a/src/com/android/telecomm/RespondViaSmsSettings.java
+++ b/src/com/android/telecomm/RespondViaSmsSettings.java
@@ -19,9 +19,7 @@
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
-import android.content.Intent;
import android.content.SharedPreferences;
-import android.content.pm.PackageManager;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
@@ -33,18 +31,6 @@
* Helper class to manage the "Respond via SMS Message" feature for incoming calls.
*/
public class RespondViaSmsSettings {
- /** SharedPreferences file name for our persistent settings. */
- private static final String SHARED_PREFERENCES_NAME = "respond_via_sms_prefs";
-
- // Preference keys for the 4 "canned responses"; see RespondViaSmsManager$Settings.
- // Since (for now at least) the number of messages is fixed at 4, and since
- // SharedPreferences can't deal with arrays anyway, just store the messages
- // as 4 separate strings.
- private static final int NUM_CANNED_RESPONSES = 4;
- private static final String KEY_CANNED_RESPONSE_PREF_1 = "canned_response_pref_1";
- private static final String KEY_CANNED_RESPONSE_PREF_2 = "canned_response_pref_2";
- private static final String KEY_CANNED_RESPONSE_PREF_3 = "canned_response_pref_3";
- private static final String KEY_CANNED_RESPONSE_PREF_4 = "canned_response_pref_4";
private static final String KEY_PREFERRED_PACKAGE = "preferred_package_pref";
private static final String KEY_INSTANT_TEXT_DEFAULT_COMPONENT = "instant_text_def_component";
@@ -63,7 +49,13 @@
super.onCreate(icicle);
Log.d(this, "Settings: onCreate()...");
- getPreferenceManager().setSharedPreferencesName(SHARED_PREFERENCES_NAME);
+ // This function guarantees that QuickResponses will be in our
+ // SharedPreferences with the proper values considering there may be
+ // old QuickResponses in Telephony pre L.
+ QuickResponseUtils.maybeMigrateLegacyQuickResponses();
+
+ getPreferenceManager().setSharedPreferencesName(
+ QuickResponseUtils.SHARED_PREFERENCES_NAME);
// This preference screen is ultra-simple; it's just 4 plain
// <EditTextPreference>s, one for each of the 4 "canned responses".
@@ -79,19 +71,23 @@
addPreferencesFromResource(R.xml.respond_via_sms_settings);
EditTextPreference pref;
- pref = (EditTextPreference) findPreference(KEY_CANNED_RESPONSE_PREF_1);
+ pref = (EditTextPreference) findPreference(
+ QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_1);
pref.setTitle(pref.getText());
pref.setOnPreferenceChangeListener(this);
- pref = (EditTextPreference) findPreference(KEY_CANNED_RESPONSE_PREF_2);
+ pref = (EditTextPreference) findPreference(
+ QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_2);
pref.setTitle(pref.getText());
pref.setOnPreferenceChangeListener(this);
- pref = (EditTextPreference) findPreference(KEY_CANNED_RESPONSE_PREF_3);
+ pref = (EditTextPreference) findPreference(
+ QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_3);
pref.setTitle(pref.getText());
pref.setOnPreferenceChangeListener(this);
- pref = (EditTextPreference) findPreference(KEY_CANNED_RESPONSE_PREF_4);
+ pref = (EditTextPreference) findPreference(
+ QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_4);
pref.setTitle(pref.getText());
pref.setOnPreferenceChangeListener(this);
@@ -130,7 +126,7 @@
case R.id.respond_via_message_reset:
// Reset the preferences settings
SharedPreferences prefs = getSharedPreferences(
- SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
+ QuickResponseUtils.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.remove(KEY_INSTANT_TEXT_DEFAULT_COMPONENT);
editor.apply();