1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.server.telecom; 18 19 import android.content.Context; 20 import android.content.SharedPreferences; 21 import android.content.pm.PackageManager; 22 import android.content.res.Resources; 23 24 // TODO: Needed for move to system service: import com.android.internal.R; 25 26 /** 27 * Utils class that exposes some helper routines to used to manage the QuickResponses 28 */ 29 public class QuickResponseUtils { 30 public static final String LOG_TAG = "QuickResponseUtils"; 31 32 // SharedPreferences file name for our persistent settings. 33 public static final String SHARED_PREFERENCES_NAME = "respond_via_sms_prefs"; 34 private static final String PACKAGE_NAME_TELEPHONY = "com.android.phone"; 35 36 // Preference keys for the 4 "canned responses"; see RespondViaSmsManager$Settings. 37 // Since (for now at least) the number of messages is fixed at 4, and since 38 // SharedPreferences can't deal with arrays anyway, just store the messages 39 // as 4 separate strings. 40 public static final int NUM_CANNED_RESPONSES = 4; 41 public static final String KEY_CANNED_RESPONSE_PREF_1 = "canned_response_pref_1"; 42 public static final String KEY_CANNED_RESPONSE_PREF_2 = "canned_response_pref_2"; 43 public static final String KEY_CANNED_RESPONSE_PREF_3 = "canned_response_pref_3"; 44 public static final String KEY_CANNED_RESPONSE_PREF_4 = "canned_response_pref_4"; 45 46 /** 47 * As of L, QuickResponses were moved from Telephony to Telecom. Because of 48 * this, we need to make sure that we migrate any old QuickResponses to our 49 * current SharedPreferences. This is a lazy migration as it happens only when 50 * the QuickResponse settings are viewed or if they are queried via RespondViaSmsManager. 51 */ maybeMigrateLegacyQuickResponses(Context context)52 public static void maybeMigrateLegacyQuickResponses(Context context) { 53 // The algorithm will go as such: 54 // If Telecom QuickResponses exist, we will skip migration because this implies 55 // that a user has already specified their desired QuickResponses and have abandoned any 56 // older QuickResponses. 57 // Then, if Telephony QuickResponses exist, we will move those to Telecom. 58 // If neither exist, we'll populate Telecom with the default QuickResponses. 59 // This guarantees the caller that QuickResponses exist in SharedPreferences after this 60 // function is called. 61 62 Log.d(LOG_TAG, "maybeMigrateLegacyQuickResponses() - Starting"); 63 final SharedPreferences prefs = context.getSharedPreferences( 64 SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE); 65 final Resources res = context.getResources(); 66 67 final boolean responsesExist = prefs.contains(KEY_CANNED_RESPONSE_PREF_1); 68 if (responsesExist) { 69 // If one QuickResponse exists, they all exist. 70 Log.d(LOG_TAG, "maybeMigrateLegacyQuickResponses() - Telecom QuickResponses exist"); 71 return; 72 } 73 74 // Grab the all the default QuickResponses from our resources. 75 String cannedResponse1 = res.getString(R.string.respond_via_sms_canned_response_1); 76 String cannedResponse2 = res.getString(R.string.respond_via_sms_canned_response_2); 77 String cannedResponse3 = res.getString(R.string.respond_via_sms_canned_response_3); 78 String cannedResponse4 = res.getString(R.string.respond_via_sms_canned_response_4); 79 80 Log.d(LOG_TAG, "maybeMigrateLegacyQuickResponses() - No local QuickResponses"); 81 82 // We don't have local QuickResponses, let's see if they live in 83 // the Telephony package and we'll fall back on using our default values. 84 Context telephonyContext = null; 85 try { 86 telephonyContext = context.createPackageContext(PACKAGE_NAME_TELEPHONY, 0); 87 } catch (PackageManager.NameNotFoundException e) { 88 Log.e(LOG_TAG, e, "maybeMigrateLegacyQuickResponses() - Can't find Telephony package."); 89 } 90 91 // Read the old canned responses from the Telephony SharedPreference if possible. 92 if (telephonyContext != null) { 93 // Note that if any one QuickResponse does not exist, we'll use the default 94 // value to populate it. 95 Log.d(LOG_TAG, "maybeMigrateLegacyQuickResponses() - Using Telephony QuickResponses."); 96 final SharedPreferences oldPrefs = telephonyContext.getSharedPreferences( 97 SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE); 98 cannedResponse1 = oldPrefs.getString(KEY_CANNED_RESPONSE_PREF_1, cannedResponse1); 99 cannedResponse2 = oldPrefs.getString(KEY_CANNED_RESPONSE_PREF_2, cannedResponse2); 100 cannedResponse3 = oldPrefs.getString(KEY_CANNED_RESPONSE_PREF_3, cannedResponse3); 101 cannedResponse4 = oldPrefs.getString(KEY_CANNED_RESPONSE_PREF_4, cannedResponse4); 102 } 103 104 // Either way, write them back into Telecom SharedPreferences. 105 final SharedPreferences.Editor editor = prefs.edit(); 106 editor.putString(KEY_CANNED_RESPONSE_PREF_1, cannedResponse1); 107 editor.putString(KEY_CANNED_RESPONSE_PREF_2, cannedResponse2); 108 editor.putString(KEY_CANNED_RESPONSE_PREF_3, cannedResponse3); 109 editor.putString(KEY_CANNED_RESPONSE_PREF_4, cannedResponse4); 110 editor.commit(); 111 112 Log.d(LOG_TAG, "maybeMigrateLegacyQuickResponses() - Done."); 113 return; 114 } 115 } 116