1/*
2 * Copyright (C) 2015 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.settings;
18
19import android.app.Activity;
20import android.app.ProgressDialog;
21import android.content.BroadcastReceiver;
22import android.content.Context;
23import android.content.Intent;
24import android.content.IntentFilter;
25import android.content.pm.ResolveInfo;
26import android.os.Bundle;
27import android.os.Handler;
28import android.os.Message;
29import android.os.PowerManager;
30import android.os.SystemClock;
31import android.os.UserManager;
32import android.provider.Settings;
33import android.util.Log;
34import android.view.View;
35import android.view.WindowManager;
36import android.view.WindowManager.LayoutParams;
37import android.view.animation.AnimationUtils;
38
39import java.util.Objects;
40
41public class FallbackHome extends Activity {
42    private static final String TAG = "FallbackHome";
43    private static final int PROGRESS_TIMEOUT = 2000;
44
45    private boolean mProvisioned;
46
47    private final Runnable mProgressTimeoutRunnable = () -> {
48        View v = getLayoutInflater().inflate(
49                R.layout.fallback_home_finishing_boot, null /* root */);
50        setContentView(v);
51        v.setAlpha(0f);
52        v.animate()
53                .alpha(1f)
54                .setDuration(500)
55                .setInterpolator(AnimationUtils.loadInterpolator(
56                        this, android.R.interpolator.fast_out_slow_in))
57                .start();
58        getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
59    };
60
61    @Override
62    protected void onCreate(Bundle savedInstanceState) {
63        super.onCreate(savedInstanceState);
64
65        // Set ourselves totally black before the device is provisioned so that
66        // we don't flash the wallpaper before SUW
67        mProvisioned = Settings.Global.getInt(getContentResolver(),
68                Settings.Global.DEVICE_PROVISIONED, 0) != 0;
69        if (!mProvisioned) {
70            setTheme(R.style.FallbackHome_SetupWizard);
71            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN
72                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
73        } else {
74            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
75                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
76        }
77
78        registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_USER_UNLOCKED));
79        maybeFinish();
80    }
81
82    @Override
83    protected void onResume() {
84        super.onResume();
85        if (mProvisioned) {
86            mHandler.postDelayed(mProgressTimeoutRunnable, PROGRESS_TIMEOUT);
87        }
88    }
89
90    @Override
91    protected void onPause() {
92        super.onPause();
93        mHandler.removeCallbacks(mProgressTimeoutRunnable);
94    }
95
96    protected void onDestroy() {
97        super.onDestroy();
98        unregisterReceiver(mReceiver);
99    }
100
101    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
102        @Override
103        public void onReceive(Context context, Intent intent) {
104            maybeFinish();
105        }
106    };
107
108    private void maybeFinish() {
109        if (getSystemService(UserManager.class).isUserUnlocked()) {
110            final Intent homeIntent = new Intent(Intent.ACTION_MAIN)
111                    .addCategory(Intent.CATEGORY_HOME);
112            final ResolveInfo homeInfo = getPackageManager().resolveActivity(homeIntent, 0);
113            if (Objects.equals(getPackageName(), homeInfo.activityInfo.packageName)) {
114                Log.d(TAG, "User unlocked but no home; let's hope someone enables one soon?");
115                mHandler.sendEmptyMessageDelayed(0, 500);
116            } else {
117                Log.d(TAG, "User unlocked and real home found; let's go!");
118                getSystemService(PowerManager.class).userActivity(
119                        SystemClock.uptimeMillis(), false);
120                finish();
121            }
122        }
123    }
124
125    private Handler mHandler = new Handler() {
126        @Override
127        public void handleMessage(Message msg) {
128            maybeFinish();
129        }
130    };
131}
132