ImportantNoticeUtils.java revision 1672ccbbb6167f434842093feaadc2bcd5634eab
1a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka/*
2a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka * Copyright (C) 2014 The Android Open Source Project
3a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka *
4a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka * Licensed under the Apache License, Version 2.0 (the "License");
5a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka * you may not use this file except in compliance with the License.
6a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka * You may obtain a copy of the License at
7a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka *
8a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka *      http://www.apache.org/licenses/LICENSE-2.0
9a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka *
10a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka * Unless required by applicable law or agreed to in writing, software
11a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka * distributed under the License is distributed on an "AS IS" BASIS,
12a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka * See the License for the specific language governing permissions and
14a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka * limitations under the License.
15a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka */
16a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka
17a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaokapackage com.android.inputmethod.latin.utils;
18a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka
19a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaokaimport android.content.Context;
20a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaokaimport android.content.SharedPreferences;
21a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaokaimport android.provider.Settings;
22a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaokaimport android.provider.Settings.SettingNotFoundException;
23a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaokaimport android.util.Log;
24a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka
25ce78a2d8ab7630cff509c2b21b4b11abd8db4795Tadashi G. Takaokaimport com.android.inputmethod.latin.InputAttributes;
265b8ffad26c9f6996fda127d0a4ddd40fb0689f0fTadashi G. Takaokaimport com.android.inputmethod.latin.R;
275b8ffad26c9f6996fda127d0a4ddd40fb0689f0fTadashi G. Takaoka
28a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaokapublic final class ImportantNoticeUtils {
29a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka    private static final String TAG = ImportantNoticeUtils.class.getSimpleName();
30a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka
31a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka    // {@link SharedPreferences} name to save the last important notice version that has been
32a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka    // displayed to users.
331672ccbbb6167f434842093feaadc2bcd5634eabTadashi G. Takaoka    private static final String PREFERENCE_NAME = "important_notice_pref";
34a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka    private static final String KEY_IMPORTANT_NOTICE_VERSION = "important_notice_version";
351672ccbbb6167f434842093feaadc2bcd5634eabTadashi G. Takaoka    public static final int VERSION_TO_ENABLE_PERSONALIZED_SUGGESTIONS = 1;
36a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka
37a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka    // Copy of the hidden {@link Settings.Secure#USER_SETUP_COMPLETE} settings key.
38a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka    // The value is zero until each multiuser completes system setup wizard.
39a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka    // Caveat: This is a hidden API.
40a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka    private static final String Settings_Secure_USER_SETUP_COMPLETE = "user_setup_complete";
41a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka    private static final int USER_SETUP_IS_NOT_COMPLETE = 0;
42a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka
43a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka    private ImportantNoticeUtils() {
44a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka        // This utility class is not publicly instantiable.
45a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka    }
46a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka
478adedbf47c7ac150d4ac7e6cdbee3ece38f346e7Jean Chalard    private static boolean isInSystemSetupWizard(final Context context) {
48a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka        try {
49a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka            final int userSetupComplete = Settings.Secure.getInt(
50a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka                    context.getContentResolver(), Settings_Secure_USER_SETUP_COMPLETE);
51a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka            return userSetupComplete == USER_SETUP_IS_NOT_COMPLETE;
52a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka        } catch (final SettingNotFoundException e) {
53a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka            Log.w(TAG, "Can't find settings in Settings.Secure: key="
54a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka                    + Settings_Secure_USER_SETUP_COMPLETE);
55a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka            return false;
56a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka        }
57a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka    }
58a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka
59a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka    private static SharedPreferences getImportantNoticePreferences(final Context context) {
60a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka        return context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
61a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka    }
62a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka
636abc852255072e9c5741a7d8f264bec99b0ce14eTadashi G. Takaoka    public static int getCurrentImportantNoticeVersion(final Context context) {
645b8ffad26c9f6996fda127d0a4ddd40fb0689f0fTadashi G. Takaoka        return context.getResources().getInteger(R.integer.config_important_notice_version);
655b8ffad26c9f6996fda127d0a4ddd40fb0689f0fTadashi G. Takaoka    }
665b8ffad26c9f6996fda127d0a4ddd40fb0689f0fTadashi G. Takaoka
671672ccbbb6167f434842093feaadc2bcd5634eabTadashi G. Takaoka    private static int getLastImportantNoticeVersion(final Context context) {
681672ccbbb6167f434842093feaadc2bcd5634eabTadashi G. Takaoka        return getImportantNoticePreferences(context).getInt(KEY_IMPORTANT_NOTICE_VERSION, 0);
691672ccbbb6167f434842093feaadc2bcd5634eabTadashi G. Takaoka    }
701672ccbbb6167f434842093feaadc2bcd5634eabTadashi G. Takaoka
711672ccbbb6167f434842093feaadc2bcd5634eabTadashi G. Takaoka    private static int getNextImportantNoticeVersion(final Context context) {
721672ccbbb6167f434842093feaadc2bcd5634eabTadashi G. Takaoka        return getLastImportantNoticeVersion(context) + 1;
731672ccbbb6167f434842093feaadc2bcd5634eabTadashi G. Takaoka    }
741672ccbbb6167f434842093feaadc2bcd5634eabTadashi G. Takaoka
75ce78a2d8ab7630cff509c2b21b4b11abd8db4795Tadashi G. Takaoka    private static boolean hasNewImportantNotice(final Context context) {
761672ccbbb6167f434842093feaadc2bcd5634eabTadashi G. Takaoka        final int lastVersion = getLastImportantNoticeVersion(context);
77ce78a2d8ab7630cff509c2b21b4b11abd8db4795Tadashi G. Takaoka        return getCurrentImportantNoticeVersion(context) > lastVersion;
78ce78a2d8ab7630cff509c2b21b4b11abd8db4795Tadashi G. Takaoka    }
79ce78a2d8ab7630cff509c2b21b4b11abd8db4795Tadashi G. Takaoka
80ce78a2d8ab7630cff509c2b21b4b11abd8db4795Tadashi G. Takaoka    public static boolean shouldShowImportantNotice(final Context context,
81ce78a2d8ab7630cff509c2b21b4b11abd8db4795Tadashi G. Takaoka            final InputAttributes inputAttributes) {
82ce78a2d8ab7630cff509c2b21b4b11abd8db4795Tadashi G. Takaoka        if (inputAttributes == null || inputAttributes.mIsPasswordField) {
83ce78a2d8ab7630cff509c2b21b4b11abd8db4795Tadashi G. Takaoka            return false;
84ce78a2d8ab7630cff509c2b21b4b11abd8db4795Tadashi G. Takaoka        }
85ce78a2d8ab7630cff509c2b21b4b11abd8db4795Tadashi G. Takaoka        return hasNewImportantNotice(context) && !isInSystemSetupWizard(context);
86a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka    }
87a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka
88a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka    public static void updateLastImportantNoticeVersion(final Context context) {
891672ccbbb6167f434842093feaadc2bcd5634eabTadashi G. Takaoka        getImportantNoticePreferences(context)
901672ccbbb6167f434842093feaadc2bcd5634eabTadashi G. Takaoka                .edit()
911672ccbbb6167f434842093feaadc2bcd5634eabTadashi G. Takaoka                .putInt(KEY_IMPORTANT_NOTICE_VERSION, getNextImportantNoticeVersion(context))
92a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka                .apply();
93a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka    }
946abc852255072e9c5741a7d8f264bec99b0ce14eTadashi G. Takaoka
951672ccbbb6167f434842093feaadc2bcd5634eabTadashi G. Takaoka    // TODO: Make title resource to string array indexed by version.
961672ccbbb6167f434842093feaadc2bcd5634eabTadashi G. Takaoka    public static String getNextImportantNoticeTitle(final Context context) {
971672ccbbb6167f434842093feaadc2bcd5634eabTadashi G. Takaoka        switch (getNextImportantNoticeVersion(context)) {
986abc852255072e9c5741a7d8f264bec99b0ce14eTadashi G. Takaoka        case VERSION_TO_ENABLE_PERSONALIZED_SUGGESTIONS:
996abc852255072e9c5741a7d8f264bec99b0ce14eTadashi G. Takaoka            return context.getString(R.string.important_notice_title);
1006abc852255072e9c5741a7d8f264bec99b0ce14eTadashi G. Takaoka        default:
1016abc852255072e9c5741a7d8f264bec99b0ce14eTadashi G. Takaoka            return null;
1026abc852255072e9c5741a7d8f264bec99b0ce14eTadashi G. Takaoka        }
1036abc852255072e9c5741a7d8f264bec99b0ce14eTadashi G. Takaoka    }
1046abc852255072e9c5741a7d8f264bec99b0ce14eTadashi G. Takaoka
1051672ccbbb6167f434842093feaadc2bcd5634eabTadashi G. Takaoka    // TODO: Make content resource to string array indexed by version.
1061672ccbbb6167f434842093feaadc2bcd5634eabTadashi G. Takaoka    public static String getNextImportantNoticeContents(final Context context) {
1071672ccbbb6167f434842093feaadc2bcd5634eabTadashi G. Takaoka        switch (getNextImportantNoticeVersion(context)) {
1086abc852255072e9c5741a7d8f264bec99b0ce14eTadashi G. Takaoka        case VERSION_TO_ENABLE_PERSONALIZED_SUGGESTIONS:
1096abc852255072e9c5741a7d8f264bec99b0ce14eTadashi G. Takaoka            return context.getString(R.string.important_notice_contents);
1106abc852255072e9c5741a7d8f264bec99b0ce14eTadashi G. Takaoka        default:
1116abc852255072e9c5741a7d8f264bec99b0ce14eTadashi G. Takaoka            return null;
1126abc852255072e9c5741a7d8f264bec99b0ce14eTadashi G. Takaoka        }
1136abc852255072e9c5741a7d8f264bec99b0ce14eTadashi G. Takaoka    }
114a14ddfb5fffff0e41620c6c00a37fb26888a95ecTadashi G. Takaoka}
115