ImmersiveModeConfirmation.java revision 34e13d90eda9bfda7a70998d190a95c88aa3d3d1
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
17package com.android.internal.policy.impl;
18
19import android.content.Context;
20import android.os.Handler;
21import android.util.ArraySet;
22import android.util.Slog;
23import android.view.View;
24import android.widget.Toast;
25
26import com.android.internal.R;
27
28/**
29 *  Helper to manage showing/hiding a confirmation prompt when the transient navigation bar
30 *  is hidden.
31 */
32public class TransientNavigationConfirmation {
33    private final String TAG = "TransientNavigationConfirmation";
34    private final boolean DEBUG = false;
35
36    private final Context mContext;
37    private final Handler mHandler;
38    private final ArraySet<String> mConfirmedUserPackages = new ArraySet<String>();
39
40    private final Runnable mHandleDismiss = new Runnable() {
41        @Override
42        public void run() {
43            if (mToast != null) {
44                mToast.cancel();
45                mToast = null;
46            }
47        }
48    };
49
50    private Toast mToast;
51    private String mLastUserPackage;
52
53    public TransientNavigationConfirmation(Context context, Handler handler) {
54        mContext = context;
55        mHandler = handler;
56    }
57
58    public void transientNavigationChanged(int userId, String pkg, boolean isNavTransient) {
59        if (pkg == null) {
60            return;
61        }
62        String userPkg = userId + ":" + pkg;
63        if (isNavTransient) {
64            mLastUserPackage = userPkg;
65            if (!mConfirmedUserPackages.contains(userPkg)) {
66                if (DEBUG) Slog.d(TAG, "Showing transient navigation confirmation for " + userPkg);
67                mHandler.post(handleShowConfirmation(userPkg));
68            }
69        } else {
70            mLastUserPackage = null;
71            if (DEBUG) Slog.d(TAG, "Hiding transient navigation confirmation for " + userPkg);
72            mHandler.post(mHandleDismiss);
73        }
74    }
75
76    public void unconfirmLastPackage() {
77        if (mLastUserPackage != null) {
78            if (DEBUG) Slog.d(TAG, "Unconfirming transient navigation for " + mLastUserPackage);
79            mConfirmedUserPackages.remove(mLastUserPackage);
80        }
81    }
82
83    private Runnable handleShowConfirmation(final String userPkg) {
84        return new Runnable() {
85            @Override
86            public void run() {
87                // create the confirmation toast bar
88                final int msg = R.string.transient_navigation_confirmation;
89                mToast = Toast.makeBar(mContext, msg, Toast.LENGTH_INFINITE);
90                mToast.setAction(R.string.ok, confirmAction(userPkg));
91
92                // we will be hiding the nav bar, so layout as if it's already hidden
93                mToast.getView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
94
95                // show the confirmation
96                mToast.show();
97            }
98        };
99    }
100
101    private Runnable confirmAction(final String userPkg) {
102        return new Runnable() {
103            @Override
104            public void run() {
105                mConfirmedUserPackages.add(userPkg);
106                mHandleDismiss.run();
107            }
108        };
109    }
110}
111