1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 package org.mozilla.gecko.tabqueue;
7 
8 import org.mozilla.gecko.GeckoSharedPrefs;
9 import org.mozilla.gecko.Locales;
10 import org.mozilla.gecko.R;
11 import org.mozilla.gecko.Telemetry;
12 import org.mozilla.gecko.TelemetryContract;
13 
14 import android.annotation.TargetApi;
15 import android.content.Intent;
16 import android.net.Uri;
17 import android.os.Build;
18 import android.os.Bundle;
19 import android.os.Handler;
20 import android.provider.Settings;
21 import android.util.Log;
22 import android.view.MotionEvent;
23 import android.view.View;
24 import android.widget.Toast;
25 
26 import android.animation.Animator;
27 import android.animation.AnimatorListenerAdapter;
28 import android.animation.AnimatorSet;
29 import android.animation.ObjectAnimator;
30 
31 public class TabQueuePrompt extends Locales.LocaleAwareActivity {
32     public static final String LOGTAG = "Gecko" + TabQueuePrompt.class.getSimpleName();
33 
34     private static final int SETTINGS_REQUEST_CODE = 1;
35 
36     // Flag set during animation to prevent animation multiple-start.
37     private boolean isAnimating;
38 
39     private View containerView;
40     private View buttonContainer;
41     private View enabledConfirmation;
42 
43     @Override
onCreate(Bundle savedInstanceState)44     protected void onCreate(Bundle savedInstanceState) {
45         super.onCreate(savedInstanceState);
46 
47         showTabQueueEnablePrompt();
48     }
49 
showTabQueueEnablePrompt()50     private void showTabQueueEnablePrompt() {
51         setContentView(R.layout.tab_queue_prompt);
52 
53         final View okButton = findViewById(R.id.ok_button);
54         okButton.setOnClickListener(new View.OnClickListener() {
55             @Override
56             public void onClick(View v) {
57                 onConfirmButtonPressed();
58                 Telemetry.sendUIEvent(TelemetryContract.Event.ACTION, TelemetryContract.Method.BUTTON, "tabqueue_prompt_yes");
59             }
60         });
61         findViewById(R.id.cancel_button).setOnClickListener(new View.OnClickListener() {
62             @Override
63             public void onClick(View v) {
64                 Telemetry.sendUIEvent(TelemetryContract.Event.ACTION, TelemetryContract.Method.BUTTON, "tabqueue_prompt_no");
65                 setResult(TabQueueHelper.TAB_QUEUE_NO);
66                 finish();
67             }
68         });
69         final View settingsButton = findViewById(R.id.settings_button);
70         settingsButton.setOnClickListener(new View.OnClickListener() {
71             @Override
72             public void onClick(View v) {
73                 onSettingsButtonPressed();
74             }
75         });
76 
77         final View tipView = findViewById(R.id.tip_text);
78         final View settingsPermitView = findViewById(R.id.settings_permit_text);
79 
80         if (TabQueueHelper.canDrawOverlays(this)) {
81             okButton.setVisibility(View.VISIBLE);
82             settingsButton.setVisibility(View.GONE);
83             tipView.setVisibility(View.VISIBLE);
84             settingsPermitView.setVisibility(View.GONE);
85         } else {
86             okButton.setVisibility(View.GONE);
87             settingsButton.setVisibility(View.VISIBLE);
88             tipView.setVisibility(View.GONE);
89             settingsPermitView.setVisibility(View.VISIBLE);
90         }
91 
92         containerView = findViewById(R.id.tab_queue_container);
93         buttonContainer = findViewById(R.id.button_container);
94         enabledConfirmation = findViewById(R.id.enabled_confirmation);
95 
96         containerView.setTranslationY(500);
97         containerView.setAlpha(0);
98 
99         final Animator translateAnimator = ObjectAnimator.ofFloat(containerView, "translationY", 0);
100         translateAnimator.setDuration(400);
101 
102         final Animator alphaAnimator = ObjectAnimator.ofFloat(containerView, "alpha", 1);
103         alphaAnimator.setStartDelay(200);
104         alphaAnimator.setDuration(600);
105 
106         final AnimatorSet set = new AnimatorSet();
107         set.playTogether(alphaAnimator, translateAnimator);
108         set.setStartDelay(400);
109 
110         set.start();
111     }
112 
113     @Override
finish()114     public void finish() {
115         super.finish();
116 
117         // Don't perform an activity-dismiss animation.
118         overridePendingTransition(0, 0);
119     }
120 
onConfirmButtonPressed()121     private void onConfirmButtonPressed() {
122         enabledConfirmation.setVisibility(View.VISIBLE);
123         enabledConfirmation.setAlpha(0);
124 
125         final Animator buttonsAlphaAnimator = ObjectAnimator.ofFloat(buttonContainer, "alpha", 0);
126         buttonsAlphaAnimator.setDuration(300);
127 
128         final Animator messagesAlphaAnimator = ObjectAnimator.ofFloat(enabledConfirmation, "alpha", 1);
129         messagesAlphaAnimator.setDuration(300);
130         messagesAlphaAnimator.setStartDelay(200);
131 
132         final AnimatorSet set = new AnimatorSet();
133         set.playTogether(buttonsAlphaAnimator, messagesAlphaAnimator);
134 
135         set.addListener(new AnimatorListenerAdapter() {
136 
137             @Override
138             public void onAnimationEnd(Animator animation) {
139 
140                 new Handler().postDelayed(new Runnable() {
141                     @Override
142                     public void run() {
143                         slideOut();
144                         setResult(TabQueueHelper.TAB_QUEUE_YES);
145                     }
146                 }, 1000);
147             }
148         });
149 
150         set.start();
151     }
152 
153     @TargetApi(Build.VERSION_CODES.M)
onSettingsButtonPressed()154     private void onSettingsButtonPressed() {
155         Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
156         intent.setData(Uri.parse("package:" + getPackageName()));
157         startActivityForResult(intent, SETTINGS_REQUEST_CODE);
158 
159         Toast.makeText(this, R.string.tab_queue_prompt_permit_drawing_over_apps, Toast.LENGTH_LONG).show();
160     }
161 
162     @Override
onActivityResult(int requestCode, int resultCode, Intent data)163     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
164         if (requestCode != SETTINGS_REQUEST_CODE) {
165             return;
166         }
167 
168         if (TabQueueHelper.canDrawOverlays(this)) {
169             // User granted the permission in Android's settings.
170             Telemetry.sendUIEvent(TelemetryContract.Event.ACTION, TelemetryContract.Method.BUTTON, "tabqueue_prompt_yes");
171 
172             setResult(TabQueueHelper.TAB_QUEUE_YES);
173             finish();
174         }
175     }
176 
177     /**
178      * Slide the overlay down off the screen and destroy it.
179      */
slideOut()180     private void slideOut() {
181         if (isAnimating) {
182             return;
183         }
184 
185         isAnimating = true;
186 
187         ObjectAnimator animator = ObjectAnimator.ofFloat(containerView, "translationY", containerView.getHeight());
188         animator.addListener(new AnimatorListenerAdapter() {
189 
190             @Override
191             public void onAnimationEnd(Animator animation) {
192                 finish();
193             }
194 
195         });
196         animator.start();
197     }
198 
199     /**
200      * Close the dialog if back is pressed.
201      */
202     @Override
onBackPressed()203     public void onBackPressed() {
204         slideOut();
205     }
206 
207     /**
208      * Close the dialog if the anything that isn't a button is tapped.
209      */
210     @Override
onTouchEvent(MotionEvent event)211     public boolean onTouchEvent(MotionEvent event) {
212         slideOut();
213         return true;
214     }
215 }