blob: fd0dbd298790a720bf90db37b04270ceef239861 [file] [log] [blame]
Anthony Leec67cd752014-09-02 23:43:44 -07001/*
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
Tyler Gunn7cc70b42014-09-12 22:17:27 -070017package com.android.server.telecom;
Anthony Leec67cd752014-09-02 23:43:44 -070018
19import android.content.Context;
20import android.content.SharedPreferences;
21import android.content.pm.PackageManager;
22import android.content.res.Resources;
23
Tyler Gunn91d43cf2014-09-17 12:19:39 -070024// TODO: Needed for move to system service: import com.android.internal.R;
Anthony Leec67cd752014-09-02 23:43:44 -070025
26/**
27 * Utils class that exposes some helper routines to used to manage the QuickResponses
28 */
29public 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 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -070047 * As of L, QuickResponses were moved from Telephony to Telecom. Because of
Anthony Leec67cd752014-09-02 23:43:44 -070048 * 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 */
Tyler Gunn91d43cf2014-09-17 12:19:39 -070052 public static void maybeMigrateLegacyQuickResponses(Context context) {
Anthony Leec67cd752014-09-02 23:43:44 -070053 // The algorithm will go as such:
Tyler Gunn7cc70b42014-09-12 22:17:27 -070054 // If Telecom QuickResponses exist, we will skip migration because this implies
Anthony Leec67cd752014-09-02 23:43:44 -070055 // that a user has already specified their desired QuickResponses and have abandoned any
56 // older QuickResponses.
Tyler Gunn7cc70b42014-09-12 22:17:27 -070057 // Then, if Telephony QuickResponses exist, we will move those to Telecom.
58 // If neither exist, we'll populate Telecom with the default QuickResponses.
Anthony Leec67cd752014-09-02 23:43:44 -070059 // This guarantees the caller that QuickResponses exist in SharedPreferences after this
60 // function is called.
61
62 Log.d(LOG_TAG, "maybeMigrateLegacyQuickResponses() - Starting");
Tyler Gunn91d43cf2014-09-17 12:19:39 -070063 final SharedPreferences prefs = context.getSharedPreferences(
Anthony Leec67cd752014-09-02 23:43:44 -070064 SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
Tyler Gunn91d43cf2014-09-17 12:19:39 -070065 final Resources res = context.getResources();
Anthony Leec67cd752014-09-02 23:43:44 -070066
Andrew Lee477ac8c2015-06-11 10:40:49 -070067 final boolean responsesExist = prefs.contains(KEY_CANNED_RESPONSE_PREF_1)
68 || prefs.contains(KEY_CANNED_RESPONSE_PREF_2)
69 || prefs.contains(KEY_CANNED_RESPONSE_PREF_3)
70 || prefs.contains(KEY_CANNED_RESPONSE_PREF_4);
Anthony Leec67cd752014-09-02 23:43:44 -070071 if (responsesExist) {
Andrew Lee477ac8c2015-06-11 10:40:49 -070072 // Skip if the user has set any canned responses.
Tyler Gunn7cc70b42014-09-12 22:17:27 -070073 Log.d(LOG_TAG, "maybeMigrateLegacyQuickResponses() - Telecom QuickResponses exist");
Anthony Leec67cd752014-09-02 23:43:44 -070074 return;
75 }
76
Anthony Leec67cd752014-09-02 23:43:44 -070077 Log.d(LOG_TAG, "maybeMigrateLegacyQuickResponses() - No local QuickResponses");
78
79 // We don't have local QuickResponses, let's see if they live in
80 // the Telephony package and we'll fall back on using our default values.
81 Context telephonyContext = null;
82 try {
Tyler Gunn91d43cf2014-09-17 12:19:39 -070083 telephonyContext = context.createPackageContext(PACKAGE_NAME_TELEPHONY, 0);
Anthony Leec67cd752014-09-02 23:43:44 -070084 } catch (PackageManager.NameNotFoundException e) {
85 Log.e(LOG_TAG, e, "maybeMigrateLegacyQuickResponses() - Can't find Telephony package.");
86 }
87
88 // Read the old canned responses from the Telephony SharedPreference if possible.
89 if (telephonyContext != null) {
Anthony Leec67cd752014-09-02 23:43:44 -070090 Log.d(LOG_TAG, "maybeMigrateLegacyQuickResponses() - Using Telephony QuickResponses.");
91 final SharedPreferences oldPrefs = telephonyContext.getSharedPreferences(
92 SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
Andrew Lee477ac8c2015-06-11 10:40:49 -070093 if (!oldPrefs.contains(KEY_CANNED_RESPONSE_PREF_1)) {
94 // Skip migration if old responses don't exist.
95 // If they exist, the first canned response should be present.
96 return;
97 }
98 String cannedResponse1 = oldPrefs.getString(KEY_CANNED_RESPONSE_PREF_1,
99 res.getString(R.string.respond_via_sms_canned_response_1));
100 String cannedResponse2 = oldPrefs.getString(KEY_CANNED_RESPONSE_PREF_2,
101 res.getString(R.string.respond_via_sms_canned_response_2));
102 String cannedResponse3 = oldPrefs.getString(KEY_CANNED_RESPONSE_PREF_3,
103 res.getString(R.string.respond_via_sms_canned_response_3));
104 String cannedResponse4 = oldPrefs.getString(KEY_CANNED_RESPONSE_PREF_4,
105 res.getString(R.string.respond_via_sms_canned_response_4));
Anthony Leec67cd752014-09-02 23:43:44 -0700106
Andrew Lee477ac8c2015-06-11 10:40:49 -0700107 // Write them into Telecom SharedPreferences.
108 final SharedPreferences.Editor editor = prefs.edit();
109 editor.putString(KEY_CANNED_RESPONSE_PREF_1, cannedResponse1);
110 editor.putString(KEY_CANNED_RESPONSE_PREF_2, cannedResponse2);
111 editor.putString(KEY_CANNED_RESPONSE_PREF_3, cannedResponse3);
112 editor.putString(KEY_CANNED_RESPONSE_PREF_4, cannedResponse4);
113 editor.commit();
114 }
Anthony Leec67cd752014-09-02 23:43:44 -0700115
116 Log.d(LOG_TAG, "maybeMigrateLegacyQuickResponses() - Done.");
117 return;
118 }
119}