Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit dc10483d authored by Chaohui Wang's avatar Chaohui Wang Committed by Android (Google) Code Review
Browse files

Merge "Move SystemUpdateManager.retrieveSystemUpdateInfo" into main

parents bf422dd6 cda836ff
Loading
Loading
Loading
Loading
+2 −15
Original line number Diff line number Diff line
@@ -16,9 +16,7 @@
package com.android.settings.system;

import android.app.settings.SettingsEnums;
import android.content.Context;
import android.os.Bundle;
import android.provider.SearchIndexableResource;

import androidx.preference.Preference;
import androidx.preference.PreferenceGroup;
@@ -29,9 +27,6 @@ import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.search.SearchIndexable;

import java.util.Arrays;
import java.util.List;

@SearchIndexable
public class SystemDashboardFragment extends DashboardFragment {

@@ -85,13 +80,5 @@ public class SystemDashboardFragment extends DashboardFragment {
     * For Search.
     */
    public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
            new BaseSearchIndexProvider() {
                @Override
                public List<SearchIndexableResource> getXmlResourcesToIndex(
                        Context context, boolean enabled) {
                    final SearchIndexableResource sir = new SearchIndexableResource(context);
                    sir.xmlResId = R.xml.system_dashboard_fragment;
                    return Arrays.asList(sir);
                }
            };
            new BaseSearchIndexProvider(R.xml.system_dashboard_fragment);
}
+42 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.system

import android.content.Context
import android.os.Bundle
import android.os.SystemUpdateManager
import android.util.Log
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

private const val TAG = "SystemUpdateManagerExt"

/**
 * Gets the system update status.
 *
 * Note: [SystemUpdateManager.retrieveSystemUpdateInfo] must be called on worker thread to avoid
 * StrictMode violation.
 */
suspend fun Context.getSystemUpdateInfo(): Bundle? = withContext(Dispatchers.Default) {
    val updateManager = getSystemService(SystemUpdateManager::class.java)!!
    try {
        updateManager.retrieveSystemUpdateInfo()
    } catch (e: Exception) {
        Log.w(TAG, "Error getting system update info.")
        null
    }
}
+0 −146
Original line number Diff line number Diff line
/*
 * 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.system;

import static android.content.Context.CARRIER_CONFIG_SERVICE;
import static android.content.Context.SYSTEM_UPDATE_SERVICE;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.os.SystemUpdateManager;
import android.os.UserManager;
import android.telephony.CarrierConfigManager;
import android.text.TextUtils;
import android.util.Log;

import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;

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

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class SystemUpdatePreferenceController extends BasePreferenceController {

    private static final String TAG = "SysUpdatePrefContr";

    private static final String KEY_SYSTEM_UPDATE_SETTINGS = "system_update_settings";

    private final UserManager mUm;
    private final SystemUpdateManager mUpdateManager;

    public SystemUpdatePreferenceController(Context context) {
        super(context, KEY_SYSTEM_UPDATE_SETTINGS);
        mUm = UserManager.get(context);
        mUpdateManager = (SystemUpdateManager) context.getSystemService(SYSTEM_UPDATE_SERVICE);
    }

    @Override
    public int getAvailabilityStatus() {
        return mContext.getResources().getBoolean(R.bool.config_show_system_update_settings)
                && mUm.isAdminUser()
                ? AVAILABLE
                : UNSUPPORTED_ON_DEVICE;
    }

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

    @Override
    public boolean handlePreferenceTreeClick(Preference preference) {
        if (TextUtils.equals(getPreferenceKey(), 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;
    }

    @Override
    public CharSequence getSummary() {
        CharSequence summary = mContext.getString(R.string.android_version_summary,
                Build.VERSION.RELEASE_OR_PREVIEW_DISPLAY);
        final FutureTask<Bundle> bundleFutureTask = new FutureTask<>(
                // Put the API call in a future to avoid StrictMode violation.
                () -> mUpdateManager.retrieveSystemUpdateInfo());
        final Bundle updateInfo;
        try {
            bundleFutureTask.run();
            updateInfo = bundleFutureTask.get();
        } catch (InterruptedException | ExecutionException e) {
            Log.w(TAG, "Error getting system update info.");
            return summary;
        }
        switch (updateInfo.getInt(SystemUpdateManager.KEY_STATUS)) {
            case SystemUpdateManager.STATUS_WAITING_DOWNLOAD:
            case SystemUpdateManager.STATUS_IN_PROGRESS:
            case SystemUpdateManager.STATUS_WAITING_INSTALL:
            case SystemUpdateManager.STATUS_WAITING_REBOOT:
                summary = mContext.getText(R.string.android_version_pending_update_summary);
                break;
            case SystemUpdateManager.STATUS_UNKNOWN:
                Log.d(TAG, "Update statue unknown");
                // fall through to next branch
            case SystemUpdateManager.STATUS_IDLE:
                final String version = updateInfo.getString(SystemUpdateManager.KEY_TITLE);
                if (!TextUtils.isEmpty(version)) {
                    summary = mContext.getString(R.string.android_version_summary, version);
                }
                break;
        }
        return summary;
    }

    /**
     * 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);
            intent.addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
            mContext.getApplicationContext().sendBroadcast(intent);
        }
    }
}
+137 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.system

import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.PersistableBundle
import android.os.SystemUpdateManager
import android.os.UserManager
import android.telephony.CarrierConfigManager
import android.util.Log
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.preference.Preference
import androidx.preference.PreferenceScreen
import com.android.settings.R
import com.android.settings.Utils
import com.android.settings.core.BasePreferenceController
import com.android.settingslib.spaprivileged.framework.common.userManager
import kotlinx.coroutines.launch

open class SystemUpdatePreferenceController(context: Context, preferenceKey: String) :
    BasePreferenceController(context, preferenceKey) {
    private val userManager: UserManager = context.userManager
    private lateinit var preference: Preference

    override fun getAvailabilityStatus() =
        if (mContext.resources.getBoolean(R.bool.config_show_system_update_settings) &&
            userManager.isAdminUser
        ) AVAILABLE else UNSUPPORTED_ON_DEVICE

    override fun displayPreference(screen: PreferenceScreen) {
        super.displayPreference(screen)
        preference = screen.findPreference(preferenceKey)!!
        if (isAvailable) {
            Utils.updatePreferenceToSpecificActivityOrRemove(
                mContext,
                screen,
                preferenceKey,
                Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY,
            )
        }
    }

    override fun handlePreferenceTreeClick(preference: Preference): Boolean {
        if (preferenceKey == preference.key) {
            val configManager = mContext.getSystemService(CarrierConfigManager::class.java)!!
            configManager.getConfig(CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_BOOL)?.let {
                if (it.getBoolean(CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_BOOL)) {
                    ciActionOnSysUpdate(it)
                }
            }
        }
        // always return false here because this handler does not want to block other handlers.
        return false
    }

    override fun onViewCreated(viewLifecycleOwner: LifecycleOwner) {
        viewLifecycleOwner.lifecycleScope.launch {
            viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
                preference.summary = calculateSummary()
            }
        }
    }

    private suspend fun calculateSummary(): String {
        val updateInfo = mContext.getSystemUpdateInfo() ?: return getReleaseVersionSummary()

        val status = updateInfo.getInt(SystemUpdateManager.KEY_STATUS)
        if (status == SystemUpdateManager.STATUS_UNKNOWN) {
            Log.d(TAG, "Update statue unknown")
        }
        when (status) {
            SystemUpdateManager.STATUS_WAITING_DOWNLOAD,
            SystemUpdateManager.STATUS_IN_PROGRESS,
            SystemUpdateManager.STATUS_WAITING_INSTALL,
            SystemUpdateManager.STATUS_WAITING_REBOOT -> {
                return mContext.getString(R.string.android_version_pending_update_summary)
            }

            SystemUpdateManager.STATUS_IDLE,
            SystemUpdateManager.STATUS_UNKNOWN -> {
                val version = updateInfo.getString(SystemUpdateManager.KEY_TITLE)
                if (!version.isNullOrEmpty()) {
                    return mContext.getString(R.string.android_version_summary, version)
                }
            }
        }
        return getReleaseVersionSummary()
    }

    private fun getReleaseVersionSummary(): String = mContext.getString(
        R.string.android_version_summary,
        Build.VERSION.RELEASE_OR_PREVIEW_DISPLAY,
    )

    /**
     * Trigger client initiated action (send intent) on system update
     */
    private fun ciActionOnSysUpdate(b: PersistableBundle) {
        val intentStr = b.getString(CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_INTENT_STRING)
        if (intentStr.isNullOrEmpty()) return
        val extra = b.getString(CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_EXTRA_STRING)
        val extraVal =
            b.getString(CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_EXTRA_VAL_STRING)
        Log.d(
            TAG,
            "ciActionOnSysUpdate: broadcasting intent $intentStr with extra $extra, $extraVal"
        )
        val intent = Intent(intentStr).apply {
            if (!extra.isNullOrEmpty()) putExtra(extra, extraVal)
            addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND)
        }
        mContext.applicationContext.sendBroadcast(intent)
    }

    companion object {
        private const val TAG = "SysUpdatePrefContr"
    }
}
+0 −1
Original line number Diff line number Diff line
@@ -53,7 +53,6 @@
    <bool name="config_show_pointer_speed">false</bool>
    <bool name="config_show_vibrate_input_devices">false</bool>
    <bool name="config_show_reset_dashboard">false</bool>
    <bool name="config_show_system_update_settings">false</bool>
    <bool name="config_show_device_model">false</bool>
    <bool name="config_show_top_level_accessibility">false</bool>
    <bool name="config_show_top_level_battery">false</bool>
Loading