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.inputmethod.latin;
18 
19 import android.app.AlertDialog;
20 import android.content.Context;
21 import android.content.DialogInterface;
22 import android.content.DialogInterface.OnClickListener;
23 
24 import com.android.inputmethod.latin.utils.DialogUtils;
25 import com.android.inputmethod.latin.utils.ImportantNoticeUtils;
26 
27 /**
28  * The dialog box that shows the important notice contents.
29  */
30 public final class ImportantNoticeDialog extends AlertDialog implements OnClickListener {
31     public interface ImportantNoticeDialogListener {
onUserAcknowledgmentOfImportantNoticeDialog(final int nextVersion)32         public void onUserAcknowledgmentOfImportantNoticeDialog(final int nextVersion);
onClickSettingsOfImportantNoticeDialog(final int nextVersion)33         public void onClickSettingsOfImportantNoticeDialog(final int nextVersion);
34     }
35 
36     private final ImportantNoticeDialogListener mListener;
37     private final int mNextImportantNoticeVersion;
38 
ImportantNoticeDialog( final Context context, final ImportantNoticeDialogListener listener)39     public ImportantNoticeDialog(
40             final Context context, final ImportantNoticeDialogListener listener) {
41         super(DialogUtils.getPlatformDialogThemeContext(context));
42         mListener = listener;
43         mNextImportantNoticeVersion = ImportantNoticeUtils.getNextImportantNoticeVersion(context);
44         setMessage(ImportantNoticeUtils.getNextImportantNoticeContents(context));
45         // Create buttons and set listeners.
46         setButton(BUTTON_POSITIVE, context.getString(android.R.string.ok), this);
47         if (shouldHaveSettingsButton()) {
48             setButton(BUTTON_NEGATIVE, context.getString(R.string.go_to_settings), this);
49         }
50         // This dialog is cancelable by pressing back key. See {@link #onBackPress()}.
51         setCancelable(true /* cancelable */);
52         setCanceledOnTouchOutside(false /* cancelable */);
53     }
54 
shouldHaveSettingsButton()55     private boolean shouldHaveSettingsButton() {
56         return mNextImportantNoticeVersion
57                 == ImportantNoticeUtils.VERSION_TO_ENABLE_PERSONALIZED_SUGGESTIONS;
58     }
59 
userAcknowledged()60     private void userAcknowledged() {
61         ImportantNoticeUtils.updateLastImportantNoticeVersion(getContext());
62         mListener.onUserAcknowledgmentOfImportantNoticeDialog(mNextImportantNoticeVersion);
63     }
64 
65     @Override
onClick(final DialogInterface dialog, final int which)66     public void onClick(final DialogInterface dialog, final int which) {
67         if (shouldHaveSettingsButton() && which == BUTTON_NEGATIVE) {
68             mListener.onClickSettingsOfImportantNoticeDialog(mNextImportantNoticeVersion);
69         }
70         userAcknowledged();
71     }
72 
73     @Override
onBackPressed()74     public void onBackPressed() {
75         super.onBackPressed();
76         userAcknowledged();
77     }
78 }
79