Home | History | Annotate | Download | only in ui
      1 /*
      2  * Copyright (C) 2013 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.research.ui;
     18 
     19 import android.app.AlertDialog.Builder;
     20 import android.app.Dialog;
     21 import android.content.DialogInterface;
     22 import android.content.DialogInterface.OnCancelListener;
     23 import android.content.Intent;
     24 import android.inputmethodservice.InputMethodService;
     25 import android.net.Uri;
     26 import android.os.IBinder;
     27 import android.view.Window;
     28 import android.view.WindowManager.LayoutParams;
     29 
     30 import com.android.inputmethod.latin.R.string;
     31 
     32 /**
     33  * Show a dialog when the user first opens the keyboard.
     34  *
     35  * The splash screen is a modal dialog box presented when the user opens this keyboard for the first
     36  * time.  It is useful for giving specific warnings that must be shown to the user before use.
     37  *
     38  * While the splash screen does share with the setup wizard the common goal of presenting
     39  * information to the user before use, they are presented at different times and with different
     40  * capabilities.  The setup wizard is launched by tapping on the icon, and walks the user through
     41  * the setup process.  It can, however, be bypassed by enabling the keyboard from Settings directly.
     42  * The splash screen cannot be bypassed, and is therefore more appropriate for obtaining user
     43  * consent.
     44  */
     45 public class SplashScreen {
     46     public interface UserConsentListener {
     47         public void onSplashScreenUserClickedOk();
     48     }
     49 
     50     final UserConsentListener mListener;
     51     final Dialog mSplashDialog;
     52 
     53     public SplashScreen(final InputMethodService inputMethodService,
     54             final UserConsentListener listener) {
     55         mListener = listener;
     56         final Builder builder = new Builder(inputMethodService)
     57                 .setTitle(string.research_splash_title)
     58                 .setMessage(string.research_splash_content)
     59                 .setPositiveButton(android.R.string.yes,
     60                         new DialogInterface.OnClickListener() {
     61                             @Override
     62                             public void onClick(DialogInterface dialog, int which) {
     63                                 mListener.onSplashScreenUserClickedOk();
     64                                 mSplashDialog.dismiss();
     65                             }
     66                 })
     67                 .setNegativeButton(android.R.string.no,
     68                         new DialogInterface.OnClickListener() {
     69                             @Override
     70                             public void onClick(DialogInterface dialog, int which) {
     71                                 final String packageName = inputMethodService.getPackageName();
     72                                 final Uri packageUri = Uri.parse("package:" + packageName);
     73                                 final Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE,
     74                                         packageUri);
     75                                 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     76                                 inputMethodService.startActivity(intent);
     77                             }
     78                 })
     79                 .setCancelable(true)
     80                 .setOnCancelListener(
     81                         new OnCancelListener() {
     82                             @Override
     83                             public void onCancel(DialogInterface dialog) {
     84                                 inputMethodService.requestHideSelf(0);
     85                             }
     86                 });
     87         mSplashDialog = builder.create();
     88     }
     89 
     90     /**
     91      * Show the splash screen.
     92      *
     93      * The user must consent to the terms presented in the SplashScreen before they can use the
     94      * keyboard.  If they cancel instead, they are given the option to uninstall the keybard.
     95      *
     96      * @param windowToken {@link IBinder} to attach dialog to
     97      */
     98     public void showSplashScreen(final IBinder windowToken) {
     99         final Window window = mSplashDialog.getWindow();
    100         final LayoutParams lp = window.getAttributes();
    101         lp.token = windowToken;
    102         lp.type = LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
    103         window.setAttributes(lp);
    104         window.addFlags(LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    105         mSplashDialog.show();
    106     }
    107 
    108     public boolean isShowing() {
    109         return mSplashDialog.isShowing();
    110     }
    111 }
    112