summaryrefslogtreecommitdiffstats
path: root/src/com/android/packageinstaller/permission/ui/wear/ConfirmationViewHandler.java
blob: 954d7e961cdc51c595c3bf6094c1369e34255c22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package com.android.packageinstaller.permission.ui.wear;

import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.Icon;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.AnimationUtils;
import android.view.animation.Interpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.android.packageinstaller.R;

public abstract class ConfirmationViewHandler implements
        Handler.Callback,
        View.OnClickListener,
        ViewTreeObserver.OnScrollChangedListener,
        ViewTreeObserver.OnGlobalLayoutListener {
    private static final String TAG = "ConfirmationViewHandler";
    
    public static final int MODE_HORIZONTAL_BUTTONS = 0;
    public static final int MODE_VERTICAL_BUTTONS = 1;

    private static final int MSG_SHOW_BUTTON_BAR = 1001;
    private static final int MSG_HIDE_BUTTON_BAR = 1002;
    private static final long HIDE_ANIM_DURATION = 500;

    private View mRoot;
    private TextView mCurrentPageText;
    private ImageView mIcon;
    private TextView mMessage;
    private ScrollView mScrollingContainer;
    private ViewGroup mContent;
    private ViewGroup mHorizontalButtonBar;
    private ViewGroup mVerticalButtonBar;
    private Button mVerticalButton1;
    private Button mVerticalButton2;
    private Button mVerticalButton3;
    private View mButtonBarContainer;

    private Context mContext;

    private Handler mHideHandler;
    private Interpolator mInterpolator;
    private float mButtonBarFloatingHeight;
    private ObjectAnimator mButtonBarAnimator;
    private float mCurrentTranslation;
    private boolean mHiddenBefore;

    // TODO: Move these into a builder
    /** In the 2 button layout, this is allow button */
    public abstract void onButton1();
    /** In the 2 button layout, this is deny button */
    public abstract void onButton2();
    public abstract void onButton3();
    public abstract CharSequence getVerticalButton1Text();
    public abstract CharSequence getVerticalButton2Text();
    public abstract CharSequence getVerticalButton3Text();
    public abstract Drawable getVerticalButton1Icon();
    public abstract Drawable getVerticalButton2Icon();
    public abstract Drawable getVerticalButton3Icon();
    public abstract CharSequence getCurrentPageText();
    public abstract Icon getPermissionIcon();
    public abstract CharSequence getMessage();

    public ConfirmationViewHandler(Context context) {
        mContext = context;
    }

    public View createView() {
        mRoot = LayoutInflater.from(mContext).inflate(R.layout.confirmation_dialog, null);

        mMessage = (TextView) mRoot.findViewById(R.id.message);
        mCurrentPageText = (TextView) mRoot.findViewById(R.id.current_page_text);
        mIcon = (ImageView) mRoot.findViewById(R.id.icon);
        mButtonBarContainer = mRoot.findViewById(R.id.button_bar_container);
        mContent = (ViewGroup) mRoot.findViewById(R.id.content);
        mScrollingContainer = (ScrollView) mRoot.findViewById(R.id.scrolling_container);
        mHorizontalButtonBar = (ViewGroup) mRoot.findViewById(R.id.horizontal_button_bar);
        mVerticalButtonBar = (ViewGroup) mRoot.findViewById(R.id.vertical_button_bar);

        Button horizontalAllow = (Button) mRoot.findViewById(R.id.permission_allow_button);
        Button horizontalDeny = (Button) mRoot.findViewById(R.id.permission_deny_button);
        horizontalAllow.setOnClickListener(this);
        horizontalDeny.setOnClickListener(this);

        mVerticalButton1 = (Button) mRoot.findViewById(R.id.vertical_button1);
        mVerticalButton2 = (Button) mRoot.findViewById(R.id.vertical_button2);
        mVerticalButton3 = (Button) mRoot.findViewById(R.id.vertical_button3);
        mVerticalButton1.setOnClickListener(this);
        mVerticalButton2.setOnClickListener(this);
        mVerticalButton3.setOnClickListener(this);

        mInterpolator = AnimationUtils.loadInterpolator(mContext,
                android.R.interpolator.fast_out_slow_in);
        mButtonBarFloatingHeight = mContext.getResources().getDimension(
                R.dimen.conf_diag_floating_height);
        mHideHandler = new Handler(Looper.getMainLooper(), this);

        mScrollingContainer.getViewTreeObserver().addOnScrollChangedListener(this);
        mRoot.getViewTreeObserver().addOnGlobalLayoutListener(this);

        return mRoot;
    }

    /**
     * Child class should override this for other modes.  Call invalidate() to update the UI to the
     * new button mode.
     * @return The current mode the layout should use for the buttons
     */
    public int getButtonBarMode() {
        return MODE_HORIZONTAL_BUTTONS;
    }

    public void invalidate() {
        CharSequence currentPageText = getCurrentPageText();
        if (!TextUtils.isEmpty(currentPageText)) {
            mCurrentPageText.setText(currentPageText);
            mCurrentPageText.setVisibility(View.VISIBLE);
        } else {
            mCurrentPageText.setVisibility(View.GONE);
        }

        Icon icon = getPermissionIcon();
        if (icon != null) {
            mIcon.setImageIcon(icon);
            mIcon.setVisibility(View.VISIBLE);
        } else {
            mIcon.setVisibility(View.GONE);
        }
        mMessage.setText(getMessage());

        switch (getButtonBarMode()) {
            case MODE_HORIZONTAL_BUTTONS:
                mHorizontalButtonBar.setVisibility(View.VISIBLE);
                mVerticalButtonBar.setVisibility(View.GONE);
                break;
            case MODE_VERTICAL_BUTTONS:
                mHorizontalButtonBar.setVisibility(View.GONE);
                mVerticalButtonBar.setVisibility(View.VISIBLE);

                mVerticalButton1.setText(getVerticalButton1Text());
                mVerticalButton2.setText(getVerticalButton2Text());

                mVerticalButton1.setCompoundDrawablesWithIntrinsicBounds(
                        getVerticalButton1Icon(), null, null, null);
                mVerticalButton2.setCompoundDrawablesWithIntrinsicBounds(
                        getVerticalButton2Icon(), null, null, null);

                CharSequence verticalButton3Text = getVerticalButton3Text();
                if (TextUtils.isEmpty(verticalButton3Text)) {
                    mVerticalButton3.setVisibility(View.GONE);
                } else {
                    mVerticalButton3.setText(getVerticalButton3Text());
                    mVerticalButton3.setCompoundDrawablesWithIntrinsicBounds(
                            getVerticalButton3Icon(), null, null, null);
                }
                break;
        }

        mScrollingContainer.scrollTo(0, 0);

        mHideHandler.removeMessages(MSG_HIDE_BUTTON_BAR);
        mHideHandler.removeMessages(MSG_SHOW_BUTTON_BAR);
    }

    @Override
    public void onGlobalLayout() {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "onGlobalLayout");
            Log.d(TAG, "    contentHeight: " + mContent.getHeight());
        }

        if (mButtonBarAnimator != null) {
            mButtonBarAnimator.cancel();
        }

        // In order to fake the buttons peeking at the bottom, need to do set the
        // padding properly.
        if (mContent.getPaddingBottom() != mButtonBarContainer.getHeight()) {
            mContent.setPadding(mContent.getPaddingLeft(), mContent.getPaddingTop(),
                    mContent.getPaddingRight(), mButtonBarContainer.getHeight());
            if (Log.isLoggable(TAG, Log.DEBUG)) {
                Log.d(TAG, "    set mContent.PaddingBottom: " + mButtonBarContainer.getHeight());
            }
        }

        mButtonBarContainer.setTranslationY(mButtonBarContainer.getHeight());

        // Give everything a chance to render
        mHideHandler.removeMessages(MSG_HIDE_BUTTON_BAR);
        mHideHandler.removeMessages(MSG_SHOW_BUTTON_BAR);
        mHideHandler.sendEmptyMessageDelayed(MSG_SHOW_BUTTON_BAR, 50);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.permission_allow_button:
            case R.id.vertical_button1:
                onButton1();
                break;
            case R.id.permission_deny_button:
            case R.id.vertical_button2:
                onButton2();
                break;
            case R.id.vertical_button3:
                onButton3();
                break;
        }
    }

    @Override
    public boolean handleMessage (Message msg) {
        switch (msg.what) {
            case MSG_SHOW_BUTTON_BAR:
                showButtonBar();
                return true;
            case MSG_HIDE_BUTTON_BAR:
                hideButtonBar();
                return true;
        }
        return false;
    }

    @Override
    public void onScrollChanged () {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "onScrollChanged");
        }
        mHideHandler.removeMessages(MSG_HIDE_BUTTON_BAR);
        hideButtonBar();
    }

    private void showButtonBar() {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "showButtonBar");
        }

        // Setup Button animation.
        // pop the button bar back to full height, stop all animation
        if (mButtonBarAnimator != null) {
            mButtonBarAnimator.cancel();
        }

        // stop any calls to hide the button bar in the future
        mHideHandler.removeMessages(MSG_HIDE_BUTTON_BAR);
        mHiddenBefore = false;

        // Evaluate the max height the button bar can go
        final int screenHeight = mRoot.getHeight();
        final int halfScreenHeight = screenHeight / 2;
        final int buttonBarHeight = mButtonBarContainer.getHeight();
        final int contentHeight = mContent.getHeight() - buttonBarHeight;
        final int buttonBarMaxHeight =
                Math.min(buttonBarHeight, halfScreenHeight);

        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "    screenHeight: " + screenHeight);
            Log.d(TAG, "    contentHeight: " + contentHeight);
            Log.d(TAG, "    buttonBarHeight: " + buttonBarHeight);
            Log.d(TAG, "    buttonBarMaxHeight: " + buttonBarMaxHeight);
        }

        mButtonBarContainer.setTranslationZ(mButtonBarFloatingHeight);

        // Only hide the button bar if it is occluding the content or the button bar is bigger than
        // half the screen
        if (contentHeight > halfScreenHeight
                || buttonBarHeight > halfScreenHeight) {
            mHideHandler.sendEmptyMessageDelayed(MSG_HIDE_BUTTON_BAR, 3000);
        }

        generateButtonBarAnimator(buttonBarHeight,
                buttonBarHeight - buttonBarMaxHeight, 0, mButtonBarFloatingHeight, 1000);
    }

    private void hideButtonBar() {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "hideButtonBar");
        }

        // The desired margin space between the button bar and the bottom of the dialog text
        final int topMargin = mContext.getResources().getDimensionPixelSize(
                R.dimen.conf_diag_button_container_top_margin);
        final int contentHeight = mContent.getHeight() + topMargin;
        final int screenHeight = mRoot.getHeight();
        final int buttonBarHeight = mButtonBarContainer.getHeight();

        final int offset = screenHeight + buttonBarHeight
                - contentHeight + Math.max(mScrollingContainer.getScrollY(), 0);
        final int translationY = (offset > 0 ?
                mButtonBarContainer.getHeight() - offset : mButtonBarContainer.getHeight());

        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "    topMargin: " + topMargin);
            Log.d(TAG, "    contentHeight: " + contentHeight);
            Log.d(TAG, "    screenHeight: " + screenHeight);
            Log.d(TAG, "    offset: " + offset);
            Log.d(TAG, "    buttonBarHeight: " + buttonBarHeight);
            Log.d(TAG, "    mContent.getPaddingBottom(): " + mContent.getPaddingBottom());
            Log.d(TAG, "    mScrollingContainer.getScrollY(): " + mScrollingContainer.getScrollY());
            Log.d(TAG, "    translationY: " + translationY);
        }

        if (!mHiddenBefore || mButtonBarAnimator == null) {
            // Remove previous call to MSG_SHOW_BUTTON_BAR if the user scrolled or something before
            // the animation got a chance to play
            mHideHandler.removeMessages(MSG_SHOW_BUTTON_BAR);

            if(mButtonBarAnimator != null) {
                mButtonBarAnimator.cancel(); // stop current animation if there is one playing
            }

            // hasn't hidden the bar yet, just hide now to the right height
            generateButtonBarAnimator(
                    mButtonBarContainer.getTranslationY(), translationY,
                    mButtonBarFloatingHeight, 0, HIDE_ANIM_DURATION);
        } else if (mButtonBarAnimator.isRunning()) {
            // we are animating the button bar closing, change to animate to the right place
            if (Math.abs(mCurrentTranslation - translationY) > 1e-2f) {
                mButtonBarAnimator.cancel(); // stop current animation

                if (Math.abs(mButtonBarContainer.getTranslationY() - translationY) > 1e-2f) {
                    long duration = Math.max((long) (
                            (float) HIDE_ANIM_DURATION
                                    * (translationY - mButtonBarContainer.getTranslationY())
                                    / mButtonBarContainer.getHeight()), 0);

                    generateButtonBarAnimator(
                            mButtonBarContainer.getTranslationY(), translationY,
                            mButtonBarFloatingHeight, 0, duration);
                } else {
                    mButtonBarContainer.setTranslationY(translationY);
                    mButtonBarContainer.setTranslationZ(0);
                }
            }
        } else {
            // not currently animating, have already hidden, snap to the right offset
            mButtonBarContainer.setTranslationY(translationY);
            mButtonBarContainer.setTranslationZ(0);
        }

        mHiddenBefore = true;
    }

    private void generateButtonBarAnimator(
            float startY, float endY, float startZ, float endZ, long duration) {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "generateButtonBarAnimator");
            Log.d(TAG, "    startY: " + startY);
            Log.d(TAG, "    endY: " + endY);
            Log.d(TAG, "    startZ: " + startZ);
            Log.d(TAG, "    endZ: " + endZ);
            Log.d(TAG, "    duration: " + duration);
        }

        mButtonBarAnimator =
                ObjectAnimator.ofPropertyValuesHolder(
                        mButtonBarContainer,
                        PropertyValuesHolder.ofFloat(View.TRANSLATION_Y, startY, endY),
                        PropertyValuesHolder.ofFloat(View.TRANSLATION_Z, startZ, endZ));
        mCurrentTranslation = endY;
        mButtonBarAnimator.setDuration(duration);
        mButtonBarAnimator.setInterpolator(mInterpolator);
        mButtonBarAnimator.start();
    }
}