1/*
2 * Copyright (C) 2017 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.deviceinfo.firmwareversion;
18
19import android.content.Context;
20import android.content.Intent;
21import android.os.Build;
22import android.os.SystemClock;
23import android.os.UserHandle;
24import android.os.UserManager;
25import android.support.annotation.VisibleForTesting;
26import android.util.Log;
27import android.view.View;
28
29import com.android.settings.R;
30import com.android.settingslib.RestrictedLockUtils;
31
32public class FirmwareVersionDialogController implements View.OnClickListener {
33
34    private static final String TAG = "firmwareDialogCtrl";
35    private static final int DELAY_TIMER_MILLIS = 500;
36    private static final int ACTIVITY_TRIGGER_COUNT = 3;
37
38    @VisibleForTesting
39    static final int FIRMWARE_VERSION_VALUE_ID = R.id.firmware_version_value;
40    @VisibleForTesting
41    static final int FIRMWARE_VERSION_LABEL_ID = R.id.firmware_version_label;
42
43    private final FirmwareVersionDialogFragment mDialog;
44    private final Context mContext;
45    private final UserManager mUserManager;
46    private final long[] mHits = new long[ACTIVITY_TRIGGER_COUNT];
47
48    private RestrictedLockUtils.EnforcedAdmin mFunDisallowedAdmin;
49    private boolean mFunDisallowedBySystem;
50
51    public FirmwareVersionDialogController(FirmwareVersionDialogFragment dialog) {
52        mDialog = dialog;
53        mContext = dialog.getContext();
54        mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
55    }
56
57    @Override
58    public void onClick(View v) {
59        arrayCopy();
60        mHits[mHits.length - 1] = SystemClock.uptimeMillis();
61        if (mHits[0] >= (SystemClock.uptimeMillis() - DELAY_TIMER_MILLIS)) {
62            if (mUserManager.hasUserRestriction(UserManager.DISALLOW_FUN)) {
63                if (mFunDisallowedAdmin != null && !mFunDisallowedBySystem) {
64                    RestrictedLockUtils.sendShowAdminSupportDetailsIntent(mContext,
65                            mFunDisallowedAdmin);
66                }
67                Log.d(TAG, "Sorry, no fun for you!");
68                return;
69            }
70
71            final Intent intent = new Intent(Intent.ACTION_MAIN)
72                    .setClassName(
73                            "android", com.android.internal.app.PlatLogoActivity.class.getName());
74            try {
75                mContext.startActivity(intent);
76            } catch (Exception e) {
77                Log.e(TAG, "Unable to start activity " + intent.toString());
78            }
79        }
80    }
81
82    /**
83     * Populates the Android version field in the dialog and registers click listeners.
84     */
85    public void initialize() {
86        initializeAdminPermissions();
87        registerClickListeners();
88
89        mDialog.setText(FIRMWARE_VERSION_VALUE_ID, Build.VERSION.RELEASE);
90    }
91
92    private void registerClickListeners() {
93        mDialog.registerClickListener(FIRMWARE_VERSION_LABEL_ID, this /* listener */);
94        mDialog.registerClickListener(FIRMWARE_VERSION_VALUE_ID, this /* listener */);
95    }
96
97    /**
98     * Copies the array onto itself to remove the oldest hit.
99     */
100    @VisibleForTesting
101    void arrayCopy() {
102        System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);
103    }
104
105    @VisibleForTesting
106    void initializeAdminPermissions() {
107        mFunDisallowedAdmin = RestrictedLockUtils.checkIfRestrictionEnforced(
108                mContext, UserManager.DISALLOW_FUN, UserHandle.myUserId());
109        mFunDisallowedBySystem = RestrictedLockUtils.hasBaseUserRestriction(
110                mContext, UserManager.DISALLOW_FUN, UserHandle.myUserId());
111    }
112}
113