1c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee/* 2c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee * Copyright (C) 2014 The Android Open Source Project 3c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee * 4c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee * Licensed under the Apache License, Version 2.0 (the "License"); 5c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee * you may not use this file except in compliance with the License. 6c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee * You may obtain a copy of the License at 7c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee * 8c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee * http://www.apache.org/licenses/LICENSE-2.0 9c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee * 10c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee * Unless required by applicable law or agreed to in writing, software 11c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee * distributed under the License is distributed on an "AS IS" BASIS, 12c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee * See the License for the specific language governing permissions and 14c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee * limitations under the License. 15c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee */ 16c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee 177cc70b4f0ad1064a4a0dce6056ad82b205887160Tyler Gunnpackage com.android.server.telecom; 18c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee 19c67cd758268dc4b18e17223cbde92e32d11715abAnthony Leeimport android.content.Context; 20c67cd758268dc4b18e17223cbde92e32d11715abAnthony Leeimport android.content.SharedPreferences; 21c67cd758268dc4b18e17223cbde92e32d11715abAnthony Leeimport android.content.pm.PackageManager; 22c67cd758268dc4b18e17223cbde92e32d11715abAnthony Leeimport android.content.res.Resources; 23a3eccfee788c3ac3c831a443b085b141b39bb63dBrad Ebingerimport android.telecom.Log; 24c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee 2591d43cf9c985cc5a83795f256ef5c46ebb8fbdc1Tyler Gunn// TODO: Needed for move to system service: import com.android.internal.R; 26c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee 27c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee/** 28c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee * Utils class that exposes some helper routines to used to manage the QuickResponses 29c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee */ 30c67cd758268dc4b18e17223cbde92e32d11715abAnthony Leepublic class QuickResponseUtils { 31c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee public static final String LOG_TAG = "QuickResponseUtils"; 32c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee 33c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee // SharedPreferences file name for our persistent settings. 34c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee public static final String SHARED_PREFERENCES_NAME = "respond_via_sms_prefs"; 35c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee private static final String PACKAGE_NAME_TELEPHONY = "com.android.phone"; 36c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee 37c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee // Preference keys for the 4 "canned responses"; see RespondViaSmsManager$Settings. 38c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee // Since (for now at least) the number of messages is fixed at 4, and since 39c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee // SharedPreferences can't deal with arrays anyway, just store the messages 40c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee // as 4 separate strings. 41c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee public static final int NUM_CANNED_RESPONSES = 4; 42c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee public static final String KEY_CANNED_RESPONSE_PREF_1 = "canned_response_pref_1"; 43c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee public static final String KEY_CANNED_RESPONSE_PREF_2 = "canned_response_pref_2"; 44c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee public static final String KEY_CANNED_RESPONSE_PREF_3 = "canned_response_pref_3"; 45c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee public static final String KEY_CANNED_RESPONSE_PREF_4 = "canned_response_pref_4"; 46c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee 47c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee /** 487cc70b4f0ad1064a4a0dce6056ad82b205887160Tyler Gunn * As of L, QuickResponses were moved from Telephony to Telecom. Because of 49c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee * this, we need to make sure that we migrate any old QuickResponses to our 50c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee * current SharedPreferences. This is a lazy migration as it happens only when 51c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee * the QuickResponse settings are viewed or if they are queried via RespondViaSmsManager. 52c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee */ 5391d43cf9c985cc5a83795f256ef5c46ebb8fbdc1Tyler Gunn public static void maybeMigrateLegacyQuickResponses(Context context) { 54c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee // The algorithm will go as such: 557cc70b4f0ad1064a4a0dce6056ad82b205887160Tyler Gunn // If Telecom QuickResponses exist, we will skip migration because this implies 56c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee // that a user has already specified their desired QuickResponses and have abandoned any 57c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee // older QuickResponses. 587cc70b4f0ad1064a4a0dce6056ad82b205887160Tyler Gunn // Then, if Telephony QuickResponses exist, we will move those to Telecom. 597cc70b4f0ad1064a4a0dce6056ad82b205887160Tyler Gunn // If neither exist, we'll populate Telecom with the default QuickResponses. 60c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee // This guarantees the caller that QuickResponses exist in SharedPreferences after this 61c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee // function is called. 62c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee 63c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee Log.d(LOG_TAG, "maybeMigrateLegacyQuickResponses() - Starting"); 6491d43cf9c985cc5a83795f256ef5c46ebb8fbdc1Tyler Gunn final SharedPreferences prefs = context.getSharedPreferences( 65c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE); 6691d43cf9c985cc5a83795f256ef5c46ebb8fbdc1Tyler Gunn final Resources res = context.getResources(); 67c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee 68477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee final boolean responsesExist = prefs.contains(KEY_CANNED_RESPONSE_PREF_1) 69477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee || prefs.contains(KEY_CANNED_RESPONSE_PREF_2) 70477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee || prefs.contains(KEY_CANNED_RESPONSE_PREF_3) 71477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee || prefs.contains(KEY_CANNED_RESPONSE_PREF_4); 72c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee if (responsesExist) { 73477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee // Skip if the user has set any canned responses. 747cc70b4f0ad1064a4a0dce6056ad82b205887160Tyler Gunn Log.d(LOG_TAG, "maybeMigrateLegacyQuickResponses() - Telecom QuickResponses exist"); 75c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee return; 76c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee } 77c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee 78c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee Log.d(LOG_TAG, "maybeMigrateLegacyQuickResponses() - No local QuickResponses"); 79c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee 80c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee // We don't have local QuickResponses, let's see if they live in 81c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee // the Telephony package and we'll fall back on using our default values. 82c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee Context telephonyContext = null; 83c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee try { 8491d43cf9c985cc5a83795f256ef5c46ebb8fbdc1Tyler Gunn telephonyContext = context.createPackageContext(PACKAGE_NAME_TELEPHONY, 0); 85c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee } catch (PackageManager.NameNotFoundException e) { 86c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee Log.e(LOG_TAG, e, "maybeMigrateLegacyQuickResponses() - Can't find Telephony package."); 87c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee } 88c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee 89c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee // Read the old canned responses from the Telephony SharedPreference if possible. 90c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee if (telephonyContext != null) { 91c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee Log.d(LOG_TAG, "maybeMigrateLegacyQuickResponses() - Using Telephony QuickResponses."); 92c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee final SharedPreferences oldPrefs = telephonyContext.getSharedPreferences( 93c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE); 94477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee if (!oldPrefs.contains(KEY_CANNED_RESPONSE_PREF_1)) { 95477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee // Skip migration if old responses don't exist. 96477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee // If they exist, the first canned response should be present. 97477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee return; 98477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee } 99477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee String cannedResponse1 = oldPrefs.getString(KEY_CANNED_RESPONSE_PREF_1, 100477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee res.getString(R.string.respond_via_sms_canned_response_1)); 101477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee String cannedResponse2 = oldPrefs.getString(KEY_CANNED_RESPONSE_PREF_2, 102477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee res.getString(R.string.respond_via_sms_canned_response_2)); 103477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee String cannedResponse3 = oldPrefs.getString(KEY_CANNED_RESPONSE_PREF_3, 104477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee res.getString(R.string.respond_via_sms_canned_response_3)); 105477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee String cannedResponse4 = oldPrefs.getString(KEY_CANNED_RESPONSE_PREF_4, 106477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee res.getString(R.string.respond_via_sms_canned_response_4)); 107c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee 108477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee // Write them into Telecom SharedPreferences. 109477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee final SharedPreferences.Editor editor = prefs.edit(); 110477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee editor.putString(KEY_CANNED_RESPONSE_PREF_1, cannedResponse1); 111477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee editor.putString(KEY_CANNED_RESPONSE_PREF_2, cannedResponse2); 112477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee editor.putString(KEY_CANNED_RESPONSE_PREF_3, cannedResponse3); 113477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee editor.putString(KEY_CANNED_RESPONSE_PREF_4, cannedResponse4); 114477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee editor.commit(); 115477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee } 116c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee 117c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee Log.d(LOG_TAG, "maybeMigrateLegacyQuickResponses() - Done."); 118c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee return; 119c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee } 120c67cd758268dc4b18e17223cbde92e32d11715abAnthony Lee} 121