ImportantNoticeUtils.java revision f96bffa69e5fa01b0bf53fb3af8c8b3539852322
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.inputmethod.latin.utils; 18 19import android.content.Context; 20import android.content.SharedPreferences; 21import android.provider.Settings; 22import android.provider.Settings.SettingNotFoundException; 23import android.text.TextUtils; 24import android.util.Log; 25 26import com.android.inputmethod.latin.InputAttributes; 27import com.android.inputmethod.latin.R; 28 29public final class ImportantNoticeUtils { 30 private static final String TAG = ImportantNoticeUtils.class.getSimpleName(); 31 32 // {@link SharedPreferences} name to save the last important notice version that has been 33 // displayed to users. 34 private static final String PREFERENCE_NAME = "important_notice_pref"; 35 private static final String KEY_IMPORTANT_NOTICE_VERSION = "important_notice_version"; 36 public static final int VERSION_TO_ENABLE_PERSONALIZED_SUGGESTIONS = 1; 37 38 // Copy of the hidden {@link Settings.Secure#USER_SETUP_COMPLETE} settings key. 39 // The value is zero until each multiuser completes system setup wizard. 40 // Caveat: This is a hidden API. 41 private static final String Settings_Secure_USER_SETUP_COMPLETE = "user_setup_complete"; 42 private static final int USER_SETUP_IS_NOT_COMPLETE = 0; 43 44 private ImportantNoticeUtils() { 45 // This utility class is not publicly instantiable. 46 } 47 48 private static boolean isInSystemSetupWizard(final Context context) { 49 try { 50 final int userSetupComplete = Settings.Secure.getInt( 51 context.getContentResolver(), Settings_Secure_USER_SETUP_COMPLETE); 52 return userSetupComplete == USER_SETUP_IS_NOT_COMPLETE; 53 } catch (final SettingNotFoundException e) { 54 Log.w(TAG, "Can't find settings in Settings.Secure: key=" 55 + Settings_Secure_USER_SETUP_COMPLETE); 56 return false; 57 } 58 } 59 60 private static SharedPreferences getImportantNoticePreferences(final Context context) { 61 return context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE); 62 } 63 64 private static int getCurrentImportantNoticeVersion(final Context context) { 65 return context.getResources().getInteger(R.integer.config_important_notice_version); 66 } 67 68 private static int getLastImportantNoticeVersion(final Context context) { 69 return getImportantNoticePreferences(context).getInt(KEY_IMPORTANT_NOTICE_VERSION, 0); 70 } 71 72 public static int getNextImportantNoticeVersion(final Context context) { 73 return getLastImportantNoticeVersion(context) + 1; 74 } 75 76 private static boolean hasNewImportantNotice(final Context context) { 77 final int lastVersion = getLastImportantNoticeVersion(context); 78 return getCurrentImportantNoticeVersion(context) > lastVersion; 79 } 80 81 public static boolean shouldShowImportantNotice(final Context context, 82 final InputAttributes inputAttributes) { 83 if (inputAttributes == null || inputAttributes.mIsPasswordField) { 84 return false; 85 } 86 if (isInSystemSetupWizard(context)) { 87 return false; 88 } 89 if (!hasNewImportantNotice(context)) { 90 return false; 91 } 92 final String importantNoticeTitle = getNextImportantNoticeTitle(context); 93 if (TextUtils.isEmpty(importantNoticeTitle)) { 94 return false; 95 } 96 return true; 97 } 98 99 public static void updateLastImportantNoticeVersion(final Context context) { 100 getImportantNoticePreferences(context) 101 .edit() 102 .putInt(KEY_IMPORTANT_NOTICE_VERSION, getNextImportantNoticeVersion(context)) 103 .apply(); 104 } 105 106 public static String getNextImportantNoticeTitle(final Context context) { 107 final int nextVersion = getCurrentImportantNoticeVersion(context); 108 final String[] importantNoticeTitleArray = context.getResources().getStringArray( 109 R.array.important_notice_title_array); 110 if (nextVersion > 0 && nextVersion < importantNoticeTitleArray.length) { 111 return importantNoticeTitleArray[nextVersion]; 112 } 113 return null; 114 } 115 116 public static String getNextImportantNoticeContents(final Context context) { 117 final int nextVersion = getNextImportantNoticeVersion(context); 118 final String[] importantNoticeContentsArray = context.getResources().getStringArray( 119 R.array.important_notice_contents_array); 120 if (nextVersion > 0 && nextVersion < importantNoticeContentsArray.length) { 121 return importantNoticeContentsArray[nextVersion]; 122 } 123 return null; 124 } 125} 126