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
17package com.android.server.telecom;
18
19import android.content.Context;
20import android.content.SharedPreferences;
21import android.content.pm.PackageManager;
22import 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 */
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    /**
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     */
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                || prefs.contains(KEY_CANNED_RESPONSE_PREF_2)
69                || prefs.contains(KEY_CANNED_RESPONSE_PREF_3)
70                || prefs.contains(KEY_CANNED_RESPONSE_PREF_4);
71        if (responsesExist) {
72            // Skip if the user has set any canned responses.
73            Log.d(LOG_TAG, "maybeMigrateLegacyQuickResponses() - Telecom QuickResponses exist");
74            return;
75        }
76
77        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 {
83            telephonyContext = context.createPackageContext(PACKAGE_NAME_TELEPHONY, 0);
84        } 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) {
90            Log.d(LOG_TAG, "maybeMigrateLegacyQuickResponses() - Using Telephony QuickResponses.");
91            final SharedPreferences oldPrefs = telephonyContext.getSharedPreferences(
92                    SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
93            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));
106
107            // 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        }
115
116        Log.d(LOG_TAG, "maybeMigrateLegacyQuickResponses() - Done.");
117        return;
118    }
119}
120