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 import android.telecom.Log;
24 
25 // TODO: Needed for move to system service: import com.android.internal.R;
26 
27 /**
28  * Utils class that exposes some helper routines to used to manage the QuickResponses
29  */
30 public class QuickResponseUtils {
31     public static final String LOG_TAG = "QuickResponseUtils";
32 
33     // SharedPreferences file name for our persistent settings.
34     public static final String SHARED_PREFERENCES_NAME = "respond_via_sms_prefs";
35     private static final String PACKAGE_NAME_TELEPHONY = "com.android.phone";
36 
37     // Preference keys for the 4 "canned responses"; see RespondViaSmsManager$Settings.
38     // Since (for now at least) the number of messages is fixed at 4, and since
39     // SharedPreferences can't deal with arrays anyway, just store the messages
40     // as 4 separate strings.
41     public static final int NUM_CANNED_RESPONSES = 4;
42     public static final String KEY_CANNED_RESPONSE_PREF_1 = "canned_response_pref_1";
43     public static final String KEY_CANNED_RESPONSE_PREF_2 = "canned_response_pref_2";
44     public static final String KEY_CANNED_RESPONSE_PREF_3 = "canned_response_pref_3";
45     public static final String KEY_CANNED_RESPONSE_PREF_4 = "canned_response_pref_4";
46 
47     /**
48      * As of L, QuickResponses were moved from Telephony to Telecom. Because of
49      * this, we need to make sure that we migrate any old QuickResponses to our
50      * current SharedPreferences.  This is a lazy migration as it happens only when
51      * the QuickResponse settings are viewed or if they are queried via RespondViaSmsManager.
52      */
maybeMigrateLegacyQuickResponses(Context context)53     public static void maybeMigrateLegacyQuickResponses(Context context) {
54         // The algorithm will go as such:
55         // If Telecom QuickResponses exist, we will skip migration because this implies
56         // that a user has already specified their desired QuickResponses and have abandoned any
57         // older QuickResponses.
58         // Then, if Telephony QuickResponses exist, we will move those to Telecom.
59         // If neither exist, we'll populate Telecom with the default QuickResponses.
60         // This guarantees the caller that QuickResponses exist in SharedPreferences after this
61         // function is called.
62 
63         Log.d(LOG_TAG, "maybeMigrateLegacyQuickResponses() - Starting");
64         final SharedPreferences prefs = context.getSharedPreferences(
65                 SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
66         final Resources res = context.getResources();
67 
68         final boolean responsesExist = prefs.contains(KEY_CANNED_RESPONSE_PREF_1)
69                 || prefs.contains(KEY_CANNED_RESPONSE_PREF_2)
70                 || prefs.contains(KEY_CANNED_RESPONSE_PREF_3)
71                 || prefs.contains(KEY_CANNED_RESPONSE_PREF_4);
72         if (responsesExist) {
73             // Skip if the user has set any canned responses.
74             Log.d(LOG_TAG, "maybeMigrateLegacyQuickResponses() - Telecom QuickResponses exist");
75             return;
76         }
77 
78         Log.d(LOG_TAG, "maybeMigrateLegacyQuickResponses() - No local QuickResponses");
79 
80         // We don't have local QuickResponses, let's see if they live in
81         // the Telephony package and we'll fall back on using our default values.
82         Context telephonyContext = null;
83         try {
84             telephonyContext = context.createPackageContext(PACKAGE_NAME_TELEPHONY, 0);
85         } catch (PackageManager.NameNotFoundException e) {
86             Log.e(LOG_TAG, e, "maybeMigrateLegacyQuickResponses() - Can't find Telephony package.");
87         }
88 
89         // Read the old canned responses from the Telephony SharedPreference if possible.
90         if (telephonyContext != null) {
91             Log.d(LOG_TAG, "maybeMigrateLegacyQuickResponses() - Using Telephony QuickResponses.");
92             final SharedPreferences oldPrefs = telephonyContext.getSharedPreferences(
93                     SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
94             if (!oldPrefs.contains(KEY_CANNED_RESPONSE_PREF_1)) {
95                 // Skip migration if old responses don't exist.
96                 // If they exist, the first canned response should be present.
97                 return;
98             }
99             String cannedResponse1 = oldPrefs.getString(KEY_CANNED_RESPONSE_PREF_1,
100                     res.getString(R.string.respond_via_sms_canned_response_1));
101             String cannedResponse2 = oldPrefs.getString(KEY_CANNED_RESPONSE_PREF_2,
102                     res.getString(R.string.respond_via_sms_canned_response_2));
103             String cannedResponse3 = oldPrefs.getString(KEY_CANNED_RESPONSE_PREF_3,
104                     res.getString(R.string.respond_via_sms_canned_response_3));
105             String cannedResponse4 = oldPrefs.getString(KEY_CANNED_RESPONSE_PREF_4,
106                     res.getString(R.string.respond_via_sms_canned_response_4));
107 
108             // Write them into Telecom SharedPreferences.
109             final SharedPreferences.Editor editor = prefs.edit();
110             editor.putString(KEY_CANNED_RESPONSE_PREF_1, cannedResponse1);
111             editor.putString(KEY_CANNED_RESPONSE_PREF_2, cannedResponse2);
112             editor.putString(KEY_CANNED_RESPONSE_PREF_3, cannedResponse3);
113             editor.putString(KEY_CANNED_RESPONSE_PREF_4, cannedResponse4);
114             editor.commit();
115         }
116 
117         Log.d(LOG_TAG, "maybeMigrateLegacyQuickResponses() - Done.");
118         return;
119     }
120 }
121