Home | History | Annotate | Download | only in retaildemo
      1 /*
      2  * Copyright (C) 2016 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.server.retaildemo;
     18 
     19 import android.app.AlertDialog;
     20 import android.app.Dialog;
     21 import android.content.Context;
     22 import android.os.CountDownTimer;
     23 import android.view.WindowManager;
     24 import android.widget.TextView;
     25 
     26 import com.android.internal.R;
     27 
     28 public class UserInactivityCountdownDialog extends AlertDialog {
     29 
     30     private OnCountDownExpiredListener mOnCountDownExpiredListener;
     31     private CountDownTimer mCountDownTimer;
     32     private long mCountDownDuration;
     33     private long mRefreshInterval;
     34 
     35     UserInactivityCountdownDialog(Context context, long duration, long refreshInterval) {
     36         super(context);
     37         mCountDownDuration = duration;
     38         mRefreshInterval = refreshInterval;
     39 
     40         getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
     41         WindowManager.LayoutParams attrs = getWindow().getAttributes();
     42         attrs.privateFlags = WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
     43         getWindow().setAttributes(attrs);
     44 
     45         setTitle(R.string.demo_user_inactivity_timeout_title);
     46         setMessage(getContext().getString(R.string.demo_user_inactivity_timeout_countdown,
     47                 duration));
     48     }
     49 
     50     public void setOnCountDownExpiredListener(
     51             OnCountDownExpiredListener onCountDownExpiredListener) {
     52         mOnCountDownExpiredListener = onCountDownExpiredListener;
     53     }
     54 
     55     public void setPositiveButtonClickListener(OnClickListener onClickListener) {
     56         setButton(Dialog.BUTTON_POSITIVE,
     57                 getContext().getString(R.string.demo_user_inactivity_timeout_right_button),
     58                 onClickListener);
     59     }
     60 
     61     public void setNegativeButtonClickListener(OnClickListener onClickListener) {
     62         setButton(Dialog.BUTTON_NEGATIVE,
     63                 getContext().getString(R.string.demo_user_inactivity_timeout_left_button),
     64                 onClickListener);
     65     }
     66 
     67     @Override
     68     public void show() {
     69         super.show();
     70         final TextView messageView = (TextView) findViewById(R.id.message);
     71         messageView.post(new Runnable() {
     72             @Override
     73             public void run() {
     74                 mCountDownTimer = new CountDownTimer(mCountDownDuration, mRefreshInterval) {
     75 
     76                     @Override
     77                     public void onTick(long millisUntilFinished) {
     78                         String msg = getContext().getString(
     79                                 R.string.demo_user_inactivity_timeout_countdown,
     80                                 millisUntilFinished / 1000);
     81                         messageView.setText(msg);
     82                     }
     83 
     84                     @Override
     85                     public void onFinish() {
     86                         dismiss();
     87                         if (mOnCountDownExpiredListener != null)
     88                             mOnCountDownExpiredListener.onCountDownExpired();
     89                     }
     90                 }.start();
     91             }
     92         });
     93     }
     94 
     95     @Override
     96     public void onStop() {
     97         if (mCountDownTimer != null) {
     98             mCountDownTimer.cancel();
     99         }
    100     }
    101 
    102     interface OnCountDownExpiredListener {
    103         void onCountDownExpired();
    104     }
    105 }
    106