summaryrefslogtreecommitdiffstats
path: root/src/com/android/settings/deviceinfo/SystemUpdatePreferenceController.java
blob: 5c2ebcb4b7d1d34ad4c651fcdf99302b5c310e17 (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
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.settings.deviceinfo;

import android.content.Context;
import android.content.Intent;
import android.os.PersistableBundle;
import android.os.UserManager;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import android.telephony.CarrierConfigManager;
import android.text.TextUtils;
import android.util.Log;

import com.android.settings.R;
import com.android.settings.Utils;
import com.android.settings.core.PreferenceController;

import java.util.List;

import static android.content.Context.CARRIER_CONFIG_SERVICE;

public class SystemUpdatePreferenceController extends PreferenceController {

    private static final String TAG = "SysUpdatePrefContr";

    static final String KEY_SYSTEM_UPDATE_SETTINGS = "system_update_settings";
    static final String KEY_UPDATE_SETTING = "additional_system_update_settings";

    private final UserManager mUm;

    public SystemUpdatePreferenceController(Context context, UserManager um) {
        super(context);
        mUm = um;
    }

    @Override
    public void displayPreference(PreferenceScreen screen) {
        if (isAvailable(mContext, KEY_SYSTEM_UPDATE_SETTINGS)) {
            Utils.updatePreferenceToSpecificActivityOrRemove(mContext, screen,
                    KEY_SYSTEM_UPDATE_SETTINGS,
                    Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
        } else {
            removePreference(screen, KEY_SYSTEM_UPDATE_SETTINGS);
        }

        if (!isAvailable(mContext, KEY_UPDATE_SETTING)) {
            removePreference(screen, KEY_UPDATE_SETTING);
        }
    }

    @Override
    public void updateNonIndexableKeys(List<String> keys) {
        // TODO: system update needs to be fixed for non-owner user b/22760654
        if (!isAvailable(mContext, KEY_SYSTEM_UPDATE_SETTINGS)) {
            keys.add(KEY_SYSTEM_UPDATE_SETTINGS);
        }
        if (!isAvailable(mContext, KEY_UPDATE_SETTING)) {
            keys.add(KEY_UPDATE_SETTING);
        }
    }

    @Override
    public boolean handlePreferenceTreeClick(Preference preference) {
        if (KEY_SYSTEM_UPDATE_SETTINGS.equals(preference.getKey())) {
            CarrierConfigManager configManager =
                    (CarrierConfigManager) mContext.getSystemService(CARRIER_CONFIG_SERVICE);
            PersistableBundle b = configManager.getConfig();
            if (b != null && b.getBoolean(CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_BOOL)) {
                ciActionOnSysUpdate(b);
            }
        }
        // always return false here because this handler does not want to block other handlers.
        return false;
    }

    /**
     * Whether a preference should be available on screen.
     */
    private boolean isAvailable(Context context, String key) {
        switch (key) {
            case KEY_SYSTEM_UPDATE_SETTINGS:
                return mUm.isAdminUser();
            case KEY_UPDATE_SETTING:
                return context.getResources().getBoolean(
                        R.bool.config_additional_system_update_setting_enable);
            default:
                return false;
        }
    }

    /**
     * Trigger client initiated action (send intent) on system update
     */
    private void ciActionOnSysUpdate(PersistableBundle b) {
        String intentStr = b.getString(CarrierConfigManager.
                KEY_CI_ACTION_ON_SYS_UPDATE_INTENT_STRING);
        if (!TextUtils.isEmpty(intentStr)) {
            String extra = b.getString(CarrierConfigManager.
                    KEY_CI_ACTION_ON_SYS_UPDATE_EXTRA_STRING);
            String extraVal = b.getString(CarrierConfigManager.
                    KEY_CI_ACTION_ON_SYS_UPDATE_EXTRA_VAL_STRING);

            Intent intent = new Intent(intentStr);
            if (!TextUtils.isEmpty(extra)) {
                intent.putExtra(extra, extraVal);
            }
            Log.d(TAG, "ciActionOnSysUpdate: broadcasting intent " + intentStr +
                    " with extra " + extra + ", " + extraVal);
            mContext.getApplicationContext().sendBroadcast(intent);
        }
    }
}