| cretin45 | d4cea55 | 2016-04-25 11:00:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The CyanogenMod Project |
| Christian Oder | cff6386 | 2020-11-09 17:14:42 +0100 | [diff] [blame] | 3 | * Copyright (C) 2017-2020 The LineageOS Project |
| cretin45 | d4cea55 | 2016-04-25 11:00:04 -0700 | [diff] [blame] | 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| Michael Bestas | c83309e | 2018-02-03 17:42:13 +0200 | [diff] [blame] | 18 | package org.lineageos.setupwizard; |
| cretin45 | d4cea55 | 2016-04-25 11:00:04 -0700 | [diff] [blame] | 19 | |
| Torsten Grote | da1355a | 2019-04-02 11:32:55 -0300 | [diff] [blame] | 20 | import static android.view.View.INVISIBLE; |
| Chirayu Desai | 0ebdabe | 2019-09-13 01:12:59 +0530 | [diff] [blame] | 21 | import static com.google.android.setupcompat.util.ResultCodes.RESULT_ACTIVITY_NOT_FOUND; |
| 22 | import static com.google.android.setupcompat.util.ResultCodes.RESULT_RETRY; |
| 23 | import static com.google.android.setupcompat.util.ResultCodes.RESULT_SKIP; |
| cretin45 | d4cea55 | 2016-04-25 11:00:04 -0700 | [diff] [blame] | 24 | |
| Michael Bestas | c83309e | 2018-02-03 17:42:13 +0200 | [diff] [blame] | 25 | import static org.lineageos.setupwizard.SetupWizardApp.ACTION_EMERGENCY_DIAL; |
| 26 | import static org.lineageos.setupwizard.SetupWizardApp.ACTION_NEXT; |
| Torsten Grote | da1355a | 2019-04-02 11:32:55 -0300 | [diff] [blame] | 27 | import static org.lineageos.setupwizard.SetupWizardApp.ACTION_SETUP_COMPLETE; |
| Michael Bestas | c83309e | 2018-02-03 17:42:13 +0200 | [diff] [blame] | 28 | import static org.lineageos.setupwizard.SetupWizardApp.EXTRA_ACTION_ID; |
| 29 | import static org.lineageos.setupwizard.SetupWizardApp.EXTRA_FIRST_RUN; |
| 30 | import static org.lineageos.setupwizard.SetupWizardApp.EXTRA_HAS_MULTIPLE_USERS; |
| 31 | import static org.lineageos.setupwizard.SetupWizardApp.EXTRA_RESULT_CODE; |
| 32 | import static org.lineageos.setupwizard.SetupWizardApp.EXTRA_SCRIPT_URI; |
| 33 | import static org.lineageos.setupwizard.SetupWizardApp.EXTRA_USE_IMMERSIVE; |
| 34 | import static org.lineageos.setupwizard.SetupWizardApp.LOGV; |
| cretin45 | d4cea55 | 2016-04-25 11:00:04 -0700 | [diff] [blame] | 35 | |
| 36 | import android.annotation.NonNull; |
| 37 | import android.annotation.Nullable; |
| 38 | import android.app.Activity; |
| 39 | import android.app.ActivityManager; |
| 40 | import android.content.ActivityNotFoundException; |
| Torsten Grote | da1355a | 2019-04-02 11:32:55 -0300 | [diff] [blame] | 41 | import android.content.BroadcastReceiver; |
| 42 | import android.content.Context; |
| cretin45 | d4cea55 | 2016-04-25 11:00:04 -0700 | [diff] [blame] | 43 | import android.content.Intent; |
| Torsten Grote | da1355a | 2019-04-02 11:32:55 -0300 | [diff] [blame] | 44 | import android.content.IntentFilter; |
| cretin45 | d4cea55 | 2016-04-25 11:00:04 -0700 | [diff] [blame] | 45 | import android.content.res.TypedArray; |
| 46 | import android.graphics.drawable.Drawable; |
| 47 | import android.net.wifi.WifiManager; |
| 48 | import android.os.Bundle; |
| 49 | import android.os.UserHandle; |
| 50 | import android.os.UserManager; |
| 51 | import android.util.Log; |
| 52 | import android.view.View; |
| Chirayu Desai | 0ebdabe | 2019-09-13 01:12:59 +0530 | [diff] [blame] | 53 | import android.view.ViewTreeObserver; |
| cretin45 | d4cea55 | 2016-04-25 11:00:04 -0700 | [diff] [blame] | 54 | import android.view.animation.Animation; |
| 55 | import android.view.animation.AnimationUtils; |
| 56 | import android.widget.Button; |
| 57 | import android.widget.ImageView; |
| 58 | import android.widget.TextView; |
| 59 | |
| Chirayu Desai | 0ebdabe | 2019-09-13 01:12:59 +0530 | [diff] [blame] | 60 | import com.google.android.setupdesign.view.NavigationBar; |
| 61 | import com.google.android.setupdesign.view.NavigationBar.NavigationBarListener; |
| 62 | import com.google.android.setupcompat.util.SystemBarHelper; |
| 63 | import com.google.android.setupcompat.util.WizardManagerHelper; |
| cretin45 | d4cea55 | 2016-04-25 11:00:04 -0700 | [diff] [blame] | 64 | |
| Michael Bestas | c83309e | 2018-02-03 17:42:13 +0200 | [diff] [blame] | 65 | import org.lineageos.setupwizard.util.SetupWizardUtils; |
| cretin45 | d4cea55 | 2016-04-25 11:00:04 -0700 | [diff] [blame] | 66 | |
| 67 | import java.util.List; |
| 68 | |
| Chirayu Desai | 0ebdabe | 2019-09-13 01:12:59 +0530 | [diff] [blame] | 69 | public abstract class BaseSetupWizardActivity extends Activity implements NavigationBarListener, |
| 70 | ViewTreeObserver.OnPreDrawListener { |
| cretin45 | d4cea55 | 2016-04-25 11:00:04 -0700 | [diff] [blame] | 71 | |
| 72 | public static final String TAG = BaseSetupWizardActivity.class.getSimpleName(); |
| 73 | |
| 74 | protected static final int TRANSITION_ID_NONE = -1; |
| 75 | protected static final int TRANSITION_ID_DEFAULT = 1; |
| 76 | protected static final int TRANSITION_ID_SLIDE = 2; |
| 77 | protected static final int TRANSITION_ID_FADE = 3; |
| 78 | |
| 79 | protected static final int NEXT_REQUEST = 10000; |
| 80 | protected static final int EMERGENCY_DIAL_ACTIVITY_REQUEST = 10038; |
| 81 | protected static final int WIFI_ACTIVITY_REQUEST = 10004; |
| 82 | protected static final int BLUETOOTH_ACTIVITY_REQUEST = 10100; |
| Chirayu Desai | e372bc0 | 2020-05-28 00:26:19 +0530 | [diff] [blame] | 83 | protected static final int BIOMETRIC_ACTIVITY_REQUEST = 10101; |
| cretin45 | d4cea55 | 2016-04-25 11:00:04 -0700 | [diff] [blame] | 84 | protected static final int SCREENLOCK_ACTIVITY_REQUEST = 10102; |
| 85 | |
| Chirayu Desai | 0ebdabe | 2019-09-13 01:12:59 +0530 | [diff] [blame] | 86 | private static final int IMMERSIVE_FLAGS = |
| 87 | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY; |
| 88 | private int mSystemUiFlags = IMMERSIVE_FLAGS | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION; |
| 89 | |
| 90 | private NavigationBar mNavigationBar; |
| cretin45 | d4cea55 | 2016-04-25 11:00:04 -0700 | [diff] [blame] | 91 | |
| 92 | protected boolean mIsActivityVisible = false; |
| 93 | protected boolean mIsExiting = false; |
| 94 | private boolean mIsFirstRun = true; |
| 95 | protected boolean mIsGoingBack = false; |
| 96 | private boolean mIsPrimaryUser; |
| Christian Oder | cff6386 | 2020-11-09 17:14:42 +0100 | [diff] [blame] | 97 | protected int mResultCode = 0; |
| cretin45 | d4cea55 | 2016-04-25 11:00:04 -0700 | [diff] [blame] | 98 | private Intent mResultData; |
| Torsten Grote | da1355a | 2019-04-02 11:32:55 -0300 | [diff] [blame] | 99 | private final BroadcastReceiver finishReceiver = new BroadcastReceiver() { |
| 100 | @Override |
| 101 | public void onReceive(Context context, Intent intent) { |
| 102 | if (ACTION_SETUP_COMPLETE.equals(intent.getAction())) { |
| 103 | if (BaseSetupWizardActivity.this instanceof FinishActivity) return; |
| 104 | if (mNavigationBar != null) { |
| 105 | // hide the activity's view, so it does not pop up again |
| 106 | mNavigationBar.getRootView().setVisibility(INVISIBLE); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | }; |
| cretin45 | d4cea55 | 2016-04-25 11:00:04 -0700 | [diff] [blame] | 111 | |
| 112 | @Override |
| 113 | protected void onCreate(@Nullable Bundle savedInstanceState) { |
| 114 | if (LOGV) { |
| 115 | logActivityState("onCreate savedInstanceState=" + savedInstanceState); |
| 116 | } |
| 117 | super.onCreate(savedInstanceState); |
| Torsten Grote | da1355a | 2019-04-02 11:32:55 -0300 | [diff] [blame] | 118 | registerReceiver(finishReceiver, new IntentFilter(ACTION_SETUP_COMPLETE)); |
| cretin45 | d4cea55 | 2016-04-25 11:00:04 -0700 | [diff] [blame] | 119 | mIsPrimaryUser = UserHandle.myUserId() == 0; |
| 120 | initLayout(); |
| Chirayu Desai | 0ebdabe | 2019-09-13 01:12:59 +0530 | [diff] [blame] | 121 | mNavigationBar = getNavigationBar(); |
| 122 | if (mNavigationBar != null) { |
| 123 | mNavigationBar.setNavigationBarListener(this); |
| 124 | mNavigationBar.addOnLayoutChangeListener((View view, |
| 125 | int left, int top, int right, int bottom, |
| 126 | int oldLeft, int oldTop, int oldRight, int oldBottom) -> { |
| 127 | view.requestApplyInsets(); |
| 128 | }); |
| 129 | mNavigationBar.setSystemUiVisibility(mSystemUiFlags); |
| 130 | // Set the UI flags before draw because the visibility might change in unexpected / |
| 131 | // undetectable times, like transitioning from a finishing activity that had a keyboard |
| 132 | ViewTreeObserver viewTreeObserver = mNavigationBar.getViewTreeObserver(); |
| 133 | viewTreeObserver.addOnPreDrawListener(this); |
| 134 | } |
| cretin45 | d4cea55 | 2016-04-25 11:00:04 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | @Override |
| 138 | protected void onStart() { |
| 139 | if (LOGV) { |
| 140 | logActivityState("onStart"); |
| 141 | } |
| 142 | super.onStart(); |
| 143 | exitIfSetupComplete(); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | protected void onRestart() { |
| 148 | if (LOGV) { |
| 149 | logActivityState("onRestart"); |
| 150 | } |
| 151 | super.onRestart(); |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | protected void onResume() { |
| 156 | if (LOGV) { |
| 157 | logActivityState("onResume"); |
| 158 | } |
| 159 | super.onResume(); |
| 160 | if (mIsGoingBack) { |
| 161 | if (!mIsExiting) { |
| 162 | applyBackwardTransition(getTransition()); |
| 163 | } |
| 164 | } else if (!mIsExiting) { |
| 165 | applyForwardTransition(getTransition()); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | protected void onPause() { |
| 171 | if (LOGV) { |
| 172 | logActivityState("onPause"); |
| 173 | } |
| 174 | super.onPause(); |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | protected void onStop() { |
| 179 | if (LOGV) { |
| 180 | logActivityState("onStop"); |
| 181 | } |
| 182 | super.onStop(); |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | protected void onDestroy() { |
| 187 | if (LOGV) { |
| 188 | logActivityState("onDestroy"); |
| 189 | } |
| Torsten Grote | da1355a | 2019-04-02 11:32:55 -0300 | [diff] [blame] | 190 | unregisterReceiver(finishReceiver); |
| cretin45 | d4cea55 | 2016-04-25 11:00:04 -0700 | [diff] [blame] | 191 | super.onDestroy(); |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public void onAttachedToWindow() { |
| 196 | if (LOGV) { |
| 197 | logActivityState("onAttachedToWindow"); |
| 198 | } |
| 199 | mIsActivityVisible = true; |
| 200 | super.onAttachedToWindow(); |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) { |
| 205 | if (LOGV) { |
| 206 | Log.v(TAG, "onRestoreInstanceState(" + savedInstanceState + ")"); |
| 207 | } |
| 208 | super.onRestoreInstanceState(savedInstanceState); |
| 209 | int currentId = savedInstanceState.getInt("currentFocus", -1); |
| 210 | if (currentId != -1) { |
| 211 | View view = findViewById(currentId); |
| 212 | if (view != null) { |
| 213 | view.requestFocus(); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | @Override |
| 219 | protected void onSaveInstanceState(@NonNull Bundle outState) { |
| 220 | super.onSaveInstanceState(outState); |
| 221 | View current = getCurrentFocus(); |
| 222 | outState.putInt("currentFocus", current != null ? current.getId() : -1); |
| 223 | if (LOGV) { |
| 224 | Log.v(TAG, "onSaveInstanceState(" + outState + ")"); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | @Override |
| Chirayu Desai | 0ebdabe | 2019-09-13 01:12:59 +0530 | [diff] [blame] | 229 | public boolean onPreDraw() { |
| 230 | // View.setSystemUiVisibility checks if the visibility changes before applying them |
| 231 | // so the performance impact is contained |
| 232 | mNavigationBar.setSystemUiVisibility(mSystemUiFlags); |
| 233 | return true; |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * @return The navigation bar instance in the layout, or null if the layout does not have a |
| 238 | * navigation bar. |
| 239 | */ |
| 240 | public NavigationBar getNavigationBar() { |
| 241 | final View view = findViewById(R.id.navigation_bar); |
| 242 | return view instanceof NavigationBar ? (NavigationBar) view : null; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Sets whether system navigation bar should be hidden. |
| 247 | * @param useImmersiveMode True to activate immersive mode and hide the system navigation bar |
| 248 | */ |
| 249 | public void setUseImmersiveMode(boolean useImmersiveMode) { |
| 250 | // By default, enable layoutHideNavigation if immersive mode is used |
| 251 | setUseImmersiveMode(useImmersiveMode, useImmersiveMode); |
| 252 | } |
| 253 | |
| 254 | public void setUseImmersiveMode(boolean useImmersiveMode, boolean layoutHideNavigation) { |
| 255 | if (useImmersiveMode) { |
| 256 | mSystemUiFlags |= IMMERSIVE_FLAGS; |
| 257 | if (layoutHideNavigation) { |
| 258 | mSystemUiFlags |= View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION; |
| cretin45 | d4cea55 | 2016-04-25 11:00:04 -0700 | [diff] [blame] | 259 | } |
| Chirayu Desai | 0ebdabe | 2019-09-13 01:12:59 +0530 | [diff] [blame] | 260 | } else { |
| 261 | mSystemUiFlags &= ~(IMMERSIVE_FLAGS | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION); |
| 262 | } |
| 263 | if (mNavigationBar != null) { |
| 264 | mNavigationBar.setSystemUiVisibility(mSystemUiFlags); |
| 265 | } |
| cretin45 | d4cea55 | 2016-04-25 11:00:04 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | protected void setBackDrawable(Drawable drawable) { |
| 269 | if (mNavigationBar != null) { |
| 270 | mNavigationBar.getBackButton().setCompoundDrawables(drawable, null, null, null); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | protected void setNextDrawable(Drawable drawable) { |
| 275 | if (mNavigationBar != null) { |
| 276 | mNavigationBar.getBackButton().setCompoundDrawables(null, null, drawable, null); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | public void setBackAllowed(boolean allowed) { |
| 281 | SystemBarHelper.setBackButtonVisible(getWindow(), allowed); |
| 282 | if (mNavigationBar != null) { |
| 283 | Button backButton = mNavigationBar.getBackButton(); |
| 284 | backButton.setEnabled(allowed); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | protected boolean isBackAllowed() { |
| 289 | if (mNavigationBar != null) { |
| 290 | mNavigationBar.getBackButton().isEnabled(); |
| 291 | } |
| 292 | return false; |
| 293 | } |
| 294 | |
| 295 | public void setNextAllowed(boolean allowed) { |
| 296 | if (mNavigationBar != null) { |
| 297 | mNavigationBar.getNextButton().setEnabled(allowed); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | protected boolean isNextAllowed() { |
| 302 | if (mNavigationBar != null) { |
| 303 | mNavigationBar.getNextButton().isEnabled(); |
| 304 | } |
| 305 | return false; |
| 306 | } |
| 307 | |
| 308 | protected void onNextPressed() { |
| 309 | nextAction(NEXT_REQUEST); |
| 310 | } |
| 311 | |
| 312 | protected void setNextText(int resId) { |
| 313 | if (mNavigationBar != null) { |
| 314 | mNavigationBar.getNextButton().setText(resId); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | protected void setBackText(int resId) { |
| 319 | if (mNavigationBar != null) { |
| 320 | mNavigationBar.getBackButton().setText(resId); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | protected void hideNextButton() { |
| 325 | if (mNavigationBar != null) { |
| 326 | Animation fadeOut = AnimationUtils.loadAnimation(this, android.R.anim.fade_out); |
| 327 | final Button next = mNavigationBar.getNextButton(); |
| 328 | next.startAnimation(fadeOut); |
| Torsten Grote | da1355a | 2019-04-02 11:32:55 -0300 | [diff] [blame] | 329 | next.setVisibility(INVISIBLE); |
| cretin45 | d4cea55 | 2016-04-25 11:00:04 -0700 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
| 333 | protected Intent getResultData() { |
| 334 | return null; |
| 335 | } |
| 336 | |
| 337 | @Override |
| 338 | public void onBackPressed() { |
| 339 | if (LOGV) { |
| 340 | Log.v(TAG, "onBackPressed()"); |
| 341 | } |
| 342 | setResultCode(RESULT_CANCELED, getResultData()); |
| 343 | super.onBackPressed(); |
| 344 | } |
| 345 | |
| 346 | public void onNavigateBack() { |
| 347 | onBackPressed(); |
| 348 | } |
| 349 | |
| 350 | public void onNavigateNext() { |
| 351 | onNextPressed(); |
| 352 | } |
| 353 | |
| 354 | protected void startEmergencyDialer() { |
| 355 | try { |
| 356 | startFirstRunActivityForResult(new Intent(ACTION_EMERGENCY_DIAL), |
| 357 | EMERGENCY_DIAL_ACTIVITY_REQUEST); |
| 358 | applyForwardTransition(TRANSITION_ID_DEFAULT); |
| 359 | } catch (ActivityNotFoundException e) { |
| 360 | Log.e(TAG, "Can't find the emergency dialer: com.android.phone.EmergencyDialer.DIAL"); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | protected void onSetupStart() { |
| 365 | SetupWizardUtils.disableCaptivePortalDetection(getApplicationContext()); |
| 366 | SetupWizardUtils.disableStatusBar(getApplicationContext()); |
| 367 | SystemBarHelper.hideSystemBars(getWindow()); |
| 368 | tryEnablingWifi(); |
| 369 | } |
| 370 | |
| 371 | |
| 372 | protected void exitIfSetupComplete() { |
| 373 | if (WizardManagerHelper.isUserSetupComplete(this)) { |
| 374 | Log.i(TAG, "Starting activity with USER_SETUP_COMPLETE=true"); |
| 375 | startSetupWizardExitActivity(); |
| 376 | setResult(RESULT_CANCELED, null); |
| 377 | finishAllAppTasks(); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | protected void finishAllAppTasks() { |
| 382 | List<ActivityManager.AppTask> appTasks = |
| 383 | getSystemService(ActivityManager.class).getAppTasks(); |
| 384 | |
| 385 | for (ActivityManager.AppTask task : appTasks) { |
| 386 | if (LOGV) { |
| 387 | Log.v(TAG, "Finishing task=" + task.toString()); |
| 388 | } |
| 389 | task.finishAndRemoveTask(); |
| 390 | } |
| 391 | finish(); |
| 392 | } |
| 393 | |
| 394 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 395 | if (LOGV) { |
| 396 | Log.v(TAG, "onActivityResult(" + getRequestName(requestCode) + ", " + |
| 397 | getResultName(requestCode, resultCode) + ")"); |
| 398 | } |
| 399 | mIsGoingBack = true; |
| 400 | if (requestCode != NEXT_REQUEST || resultCode != RESULT_CANCELED) { |
| 401 | if (requestCode == EMERGENCY_DIAL_ACTIVITY_REQUEST) { |
| 402 | applyBackwardTransition(TRANSITION_ID_DEFAULT); |
| 403 | return; |
| 404 | } |
| 405 | if (resultCode == RESULT_CANCELED) { |
| 406 | finish(); |
| 407 | } else { |
| 408 | nextAction(resultCode); |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | public void finish() { |
| 414 | if (LOGV) { |
| 415 | Log.v(TAG, "finish"); |
| 416 | } |
| 417 | super.finish(); |
| 418 | if (isResumed() && mResultCode == RESULT_CANCELED) { |
| 419 | applyBackwardTransition(getTransition()); |
| 420 | } |
| 421 | mIsExiting = true; |
| 422 | } |
| 423 | |
| 424 | protected void finishAction() { |
| 425 | finishAction(RESULT_CANCELED); |
| 426 | } |
| 427 | |
| 428 | protected void finishAction(int resultCode) { |
| 429 | finishAction(resultCode, null); |
| 430 | } |
| 431 | |
| 432 | protected void finishAction(int resultCode, Intent data) { |
| 433 | if (resultCode != 0) { |
| 434 | nextAction(resultCode, data); |
| 435 | } |
| 436 | finish(); |
| 437 | } |
| 438 | |
| 439 | protected void setResultCode(int resultCode) { |
| 440 | setResultCode(resultCode, getResultData()); |
| 441 | } |
| 442 | |
| 443 | protected void setResultCode(int resultCode, Intent data) { |
| 444 | if (LOGV) { |
| 445 | Log.v(TAG, "setResultCode result=" + getResultName(0, resultCode) + " data=" + data); |
| 446 | } |
| 447 | mResultCode = resultCode; |
| 448 | mResultData = data; |
| 449 | setResult(resultCode, data); |
| 450 | } |
| 451 | |
| 452 | protected void nextAction(int resultCode) { |
| 453 | nextAction(resultCode, null); |
| 454 | } |
| 455 | |
| 456 | protected void nextAction(int resultCode, Intent data) { |
| 457 | if (LOGV) { |
| 458 | Log.v(TAG, "nextAction resultCode=" + resultCode + |
| 459 | " data=" + data + " this=" + this); |
| 460 | } |
| 461 | if (resultCode == 0) { |
| 462 | throw new IllegalArgumentException("Cannot call nextAction with RESULT_CANCELED"); |
| 463 | } |
| 464 | setResultCode(resultCode, data); |
| 465 | sendActionResults(); |
| 466 | } |
| 467 | |
| 468 | public void startActivity(Intent intent) { |
| 469 | super.startActivity(intent); |
| 470 | if (isResumed() && mIsActivityVisible) { |
| 471 | applyForwardTransition(getTransition()); |
| 472 | } |
| 473 | mIsExiting = true; |
| 474 | } |
| 475 | |
| 476 | public void startActivityForResult(Intent intent, int requestCode) { |
| 477 | super.startActivityForResult(intent, requestCode); |
| 478 | if (isResumed() && mIsActivityVisible) { |
| 479 | applyForwardTransition(getTransition()); |
| 480 | } |
| 481 | mIsExiting = true; |
| 482 | } |
| 483 | |
| 484 | protected void sendActionResults() { |
| 485 | if (LOGV) { |
| 486 | Log.v(TAG, "sendActionResults resultCode=" + mResultCode + " data=" + mResultData); |
| 487 | } |
| 488 | Intent intent = new Intent(ACTION_NEXT); |
| 489 | intent.putExtra(EXTRA_SCRIPT_URI, getIntent().getStringExtra(EXTRA_SCRIPT_URI)); |
| 490 | intent.putExtra(EXTRA_ACTION_ID, getIntent().getStringExtra(EXTRA_ACTION_ID)); |
| 491 | intent.putExtra(EXTRA_RESULT_CODE, mResultCode); |
| 492 | if (!(mResultData == null || mResultData.getExtras() == null)) { |
| 493 | intent.putExtras(mResultData.getExtras()); |
| 494 | } |
| 495 | startActivityForResult(intent, NEXT_REQUEST); |
| 496 | } |
| 497 | |
| 498 | protected void applyForwardTransition(int transitionId) { |
| 499 | if (transitionId == TRANSITION_ID_SLIDE) { |
| Chirayu Desai | 0ebdabe | 2019-09-13 01:12:59 +0530 | [diff] [blame] | 500 | overridePendingTransition(R.anim.sud_slide_next_in, R.anim.sud_slide_next_out); |
| cretin45 | d4cea55 | 2016-04-25 11:00:04 -0700 | [diff] [blame] | 501 | } else if (transitionId == TRANSITION_ID_FADE) { |
| 502 | overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out); |
| 503 | } else if (transitionId == TRANSITION_ID_DEFAULT) { |
| 504 | TypedArray typedArray = obtainStyledAttributes(android.R.style.Animation_Activity, |
| 505 | new int[]{android.R.attr.activityOpenEnterAnimation, |
| 506 | android.R.attr.activityOpenExitAnimation}); |
| 507 | overridePendingTransition(typedArray.getResourceId(0, 0), |
| 508 | typedArray.getResourceId(1, 0)); |
| 509 | typedArray.recycle(); |
| 510 | } else if (transitionId == TRANSITION_ID_NONE) { |
| 511 | overridePendingTransition(0, 0); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | protected void applyBackwardTransition(int transitionId) { |
| 516 | if (transitionId == TRANSITION_ID_SLIDE) { |
| Chirayu Desai | 0ebdabe | 2019-09-13 01:12:59 +0530 | [diff] [blame] | 517 | overridePendingTransition(R.anim.sud_slide_back_in, R.anim.sud_slide_back_out); |
| cretin45 | d4cea55 | 2016-04-25 11:00:04 -0700 | [diff] [blame] | 518 | } else if (transitionId == TRANSITION_ID_FADE) { |
| 519 | overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out); |
| 520 | } else if (transitionId == TRANSITION_ID_DEFAULT) { |
| 521 | TypedArray typedArray = obtainStyledAttributes(android.R.style.Animation_Activity, |
| 522 | new int[]{android.R.attr.activityCloseEnterAnimation, |
| 523 | android.R.attr.activityCloseExitAnimation}); |
| 524 | overridePendingTransition(typedArray.getResourceId(0, 0), |
| 525 | typedArray.getResourceId(1, 0)); |
| 526 | typedArray.recycle(); |
| 527 | } else if (transitionId == TRANSITION_ID_NONE) { |
| 528 | overridePendingTransition(0, 0); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | protected void hideBackButton() { |
| 533 | if (mNavigationBar != null) { |
| 534 | Animation fadeOut = AnimationUtils.loadAnimation(this, android.R.anim.fade_out); |
| 535 | final Button back = mNavigationBar.getBackButton(); |
| 536 | back.startAnimation(fadeOut); |
| Torsten Grote | da1355a | 2019-04-02 11:32:55 -0300 | [diff] [blame] | 537 | back.setVisibility(INVISIBLE); |
| cretin45 | d4cea55 | 2016-04-25 11:00:04 -0700 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | |
| 541 | protected int getTransition() { |
| Christian Oder | cff6386 | 2020-11-09 17:14:42 +0100 | [diff] [blame] | 542 | return TRANSITION_ID_SLIDE; |
| cretin45 | d4cea55 | 2016-04-25 11:00:04 -0700 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | protected boolean tryEnablingWifi() { |
| 546 | WifiManager wifiManager = getSystemService(WifiManager.class); |
| 547 | if (wifiManager != null && !wifiManager.isWifiEnabled()) { |
| 548 | return wifiManager.setWifiEnabled(true); |
| 549 | } |
| 550 | return false; |
| 551 | } |
| 552 | |
| 553 | private void startSetupWizardExitActivity() { |
| 554 | if (LOGV) { |
| 555 | Log.v(TAG, "startSetupWizardExitActivity()"); |
| 556 | } |
| 557 | startFirstRunActivity(new Intent(this, SetupWizardExitActivity.class)); |
| 558 | } |
| 559 | |
| 560 | protected void startFirstRunActivity(Intent intent) { |
| 561 | if (LOGV) { |
| 562 | Log.v(TAG, "starting activity " + intent); |
| 563 | } |
| 564 | intent.putExtra(EXTRA_FIRST_RUN, isFirstRun()); |
| 565 | intent.putExtra(EXTRA_HAS_MULTIPLE_USERS, hasMultipleUsers()); |
| 566 | intent.putExtra(EXTRA_USE_IMMERSIVE, true); |
| 567 | startActivity(intent); |
| 568 | } |
| 569 | |
| 570 | protected void startFirstRunActivityForResult(Intent intent, int requestCode) { |
| 571 | if (LOGV) { |
| 572 | Log.v(TAG, "startFirstRunActivityForResult requestCode=" + requestCode); |
| 573 | } |
| 574 | intent.putExtra(EXTRA_FIRST_RUN, isFirstRun()); |
| 575 | intent.putExtra(EXTRA_HAS_MULTIPLE_USERS, hasMultipleUsers()); |
| 576 | intent.putExtra(EXTRA_USE_IMMERSIVE, true); |
| 577 | startActivityForResult(intent, requestCode); |
| 578 | } |
| 579 | |
| 580 | |
| 581 | protected boolean isFirstRun() { |
| 582 | return mIsFirstRun; |
| 583 | } |
| 584 | |
| 585 | protected boolean isPrimaryUser() { |
| 586 | return mIsPrimaryUser; |
| 587 | } |
| 588 | |
| 589 | public boolean hasMultipleUsers() { |
| 590 | return ((UserManager) getSystemService(USER_SERVICE)).getUsers().size() > 1; |
| 591 | } |
| 592 | |
| 593 | protected void logActivityState(String prefix) { |
| 594 | Log.v(TAG, prefix + " isResumed=" + isResumed() + " isFinishing=" + |
| 595 | isFinishing() + " isDestroyed=" + isDestroyed()); |
| 596 | } |
| 597 | |
| 598 | protected static String getRequestName(int requestCode) { |
| 599 | StringBuilder sb = new StringBuilder(); |
| 600 | switch (requestCode) { |
| 601 | case NEXT_REQUEST: |
| 602 | sb.append("NEXT_REQUEST"); |
| 603 | break; |
| 604 | case EMERGENCY_DIAL_ACTIVITY_REQUEST: |
| 605 | sb.append("EMERGENCY_DIAL_ACTIVITY_REQUEST"); |
| 606 | break; |
| 607 | case WIFI_ACTIVITY_REQUEST: |
| 608 | sb.append("WIFI_ACTIVITY_REQUEST"); |
| 609 | break; |
| 610 | case BLUETOOTH_ACTIVITY_REQUEST: |
| 611 | sb.append("BLUETOOTH_ACTIVITY_REQUEST"); |
| 612 | break; |
| Chirayu Desai | e372bc0 | 2020-05-28 00:26:19 +0530 | [diff] [blame] | 613 | case BIOMETRIC_ACTIVITY_REQUEST: |
| 614 | sb.append("BIOMETRIC_ACTIVITY_REQUEST"); |
| cretin45 | d4cea55 | 2016-04-25 11:00:04 -0700 | [diff] [blame] | 615 | break; |
| 616 | case SCREENLOCK_ACTIVITY_REQUEST: |
| 617 | sb.append("SCREENLOCK_ACTIVITY_REQUEST"); |
| 618 | break; |
| 619 | } |
| 620 | sb.append("(").append(requestCode).append(")"); |
| 621 | return sb.toString(); |
| 622 | } |
| 623 | |
| 624 | protected static String getResultName(int requestCode, int resultCode) { |
| 625 | StringBuilder sb = new StringBuilder(); |
| 626 | switch (requestCode) { |
| 627 | case WIFI_ACTIVITY_REQUEST: |
| 628 | switch (resultCode) { |
| 629 | case RESULT_OK: |
| 630 | sb.append("RESULT_OK"); |
| 631 | break; |
| 632 | case RESULT_CANCELED: |
| 633 | sb.append("RESULT_CANCELED"); |
| 634 | break; |
| 635 | case RESULT_SKIP: |
| 636 | sb.append("RESULT_WIFI_SKIP"); |
| 637 | break; |
| 638 | default: |
| 639 | break; |
| 640 | } |
| 641 | case BLUETOOTH_ACTIVITY_REQUEST: |
| 642 | switch (resultCode) { |
| 643 | case RESULT_OK: |
| 644 | sb.append("RESULT_OK"); |
| 645 | break; |
| 646 | case RESULT_CANCELED: |
| 647 | sb.append("RESULT_CANCELED"); |
| 648 | break; |
| 649 | case RESULT_SKIP: |
| 650 | sb.append("RESULT_BLUETOOTH_SKIP"); |
| 651 | break; |
| 652 | default: |
| 653 | break; |
| 654 | } |
| Chirayu Desai | e372bc0 | 2020-05-28 00:26:19 +0530 | [diff] [blame] | 655 | case BIOMETRIC_ACTIVITY_REQUEST: |
| cretin45 | d4cea55 | 2016-04-25 11:00:04 -0700 | [diff] [blame] | 656 | switch (resultCode) { |
| 657 | case RESULT_OK: |
| 658 | sb.append("RESULT_OK"); |
| 659 | break; |
| 660 | case RESULT_CANCELED: |
| 661 | sb.append("RESULT_CANCELED"); |
| 662 | break; |
| 663 | case RESULT_SKIP: |
| Chirayu Desai | e372bc0 | 2020-05-28 00:26:19 +0530 | [diff] [blame] | 664 | sb.append("RESULT_BIOMETRIC_SKIP"); |
| cretin45 | d4cea55 | 2016-04-25 11:00:04 -0700 | [diff] [blame] | 665 | break; |
| 666 | default: |
| 667 | break; |
| 668 | } |
| 669 | case SCREENLOCK_ACTIVITY_REQUEST: |
| 670 | switch (resultCode) { |
| 671 | case RESULT_OK: |
| 672 | sb.append("RESULT_OK"); |
| 673 | break; |
| 674 | case RESULT_CANCELED: |
| 675 | sb.append("RESULT_CANCELED"); |
| 676 | break; |
| 677 | case RESULT_SKIP: |
| 678 | sb.append("RESULT_SCREENLOCK_SKIP"); |
| 679 | break; |
| 680 | default: |
| 681 | break; |
| 682 | } |
| 683 | default: |
| 684 | switch (resultCode) { |
| 685 | case RESULT_OK: |
| 686 | sb.append("RESULT_OK"); |
| 687 | break; |
| 688 | case RESULT_CANCELED: |
| 689 | sb.append("RESULT_CANCELED"); |
| 690 | break; |
| 691 | case RESULT_SKIP: |
| 692 | sb.append("RESULT_SKIP"); |
| 693 | break; |
| 694 | case RESULT_RETRY: |
| 695 | sb.append("RESULT_RETRY"); |
| 696 | break; |
| 697 | case RESULT_ACTIVITY_NOT_FOUND: |
| 698 | sb.append("RESULT_ACTIVITY_NOT_FOUND"); |
| 699 | break; |
| 700 | } |
| 701 | break; |
| 702 | } |
| 703 | sb.append("(").append(resultCode).append(")"); |
| 704 | return sb.toString(); |
| 705 | } |
| 706 | |
| 707 | private void initLayout() { |
| 708 | if (getLayoutResId() != -1) { |
| 709 | setContentView(getLayoutResId()); |
| 710 | } |
| 711 | if (getTitleResId() != -1) { |
| 712 | TextView title = (TextView) findViewById(android.R.id.title); |
| 713 | title.setText(getTitleResId()); |
| 714 | } |
| 715 | if (getIconResId() != -1) { |
| 716 | ImageView icon = (ImageView) findViewById(R.id.header_icon); |
| 717 | icon.setImageResource(getIconResId()); |
| 718 | icon.setVisibility(View.VISIBLE); |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | protected int getLayoutResId() { |
| 723 | return -1; |
| 724 | } |
| 725 | |
| 726 | protected int getTitleResId() { |
| 727 | return -1; |
| 728 | } |
| 729 | |
| 730 | protected int getIconResId() { |
| 731 | return -1; |
| 732 | } |
| 733 | } |