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

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

Merge changes I51395a55,I63a86569 into main

* changes:
  Fix unable to erase eSIM
  SubscriptionRepository.activeSubscriptionIdListFlow
parents ad1fa9b0 b70c8057
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -52,6 +52,7 @@ import com.android.settings.network.SubscriptionUtil;
import com.android.settings.network.telephony.EuiccRacConnectivityDialogActivity;
import com.android.settings.password.ChooseLockSettingsHelper;
import com.android.settings.password.ConfirmLockPattern;
import com.android.settings.system.reset.ResetNetworkConfirm;
import com.android.settingslib.development.DevelopmentSettingsEnabler;

import java.util.ArrayList;
+0 −247
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015 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;

import android.app.Activity;
import android.app.ProgressDialog;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Looper;
import android.telephony.SubscriptionManager;
import android.telephony.SubscriptionManager.OnSubscriptionsChangedListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.VisibleForTesting;
import androidx.appcompat.app.AlertDialog;

import com.android.settings.core.InstrumentedFragment;
import com.android.settings.network.ResetNetworkOperationBuilder;
import com.android.settings.network.ResetNetworkRestrictionViewBuilder;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Confirm and execute a reset of the network settings to a clean "just out of the box"
 * state.  Multiple confirmations are required: first, a general "are you sure
 * you want to do this?" prompt, followed by a keyguard pattern trace if the user
 * has defined one, followed by a final strongly-worded "THIS WILL RESET EVERYTHING"
 * prompt.  If at any time the phone is allowed to go to sleep, is
 * locked, et cetera, then the confirmation sequence is abandoned.
 *
 * This is the confirmation screen.
 */
public class ResetNetworkConfirm extends InstrumentedFragment {
    private static final String TAG = "ResetNetworkConfirm";

    @VisibleForTesting View mContentView;
    @VisibleForTesting ResetNetworkTask mResetNetworkTask;
    @VisibleForTesting Activity mActivity;
    @VisibleForTesting ResetNetworkRequest mResetNetworkRequest;
    private ProgressDialog mProgressDialog;
    private AlertDialog mAlertDialog;
    @VisibleForTesting ResetSubscriptionContract mResetSubscriptionContract;
    private OnSubscriptionsChangedListener mSubscriptionsChangedListener;

    /**
     * Async task used to do all reset task. If error happens during
     * erasing eSIM profiles or timeout, an error msg is shown.
     */
    private class ResetNetworkTask extends AsyncTask<Void, Void, Boolean> {
        private static final String TAG = "ResetNetworkTask";

        private final Context mContext;

        ResetNetworkTask(Context context) {
            mContext = context;
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            final AtomicBoolean resetEsimSuccess = new AtomicBoolean(true);

            String resetEsimPackageName = mResetNetworkRequest.getResetEsimPackageName();
            ResetNetworkOperationBuilder builder = mResetNetworkRequest
                    .toResetNetworkOperationBuilder(mContext, Looper.getMainLooper());
            if (resetEsimPackageName != null) {
                // Override reset eSIM option for the result of reset operation
                builder = builder.resetEsim(resetEsimPackageName,
                        success -> { resetEsimSuccess.set(success); }
                        );
            }
            builder.build().run();

            boolean isResetSucceed = resetEsimSuccess.get();
            Log.d(TAG, "network factoryReset complete. succeeded: "
                    + String.valueOf(isResetSucceed));
            return isResetSucceed;
        }

        @Override
        protected void onPostExecute(Boolean succeeded) {
            if (mProgressDialog != null && mProgressDialog.isShowing()) {
                mProgressDialog.dismiss();
            }

            if (succeeded) {
                Toast.makeText(mContext, R.string.reset_network_complete_toast, Toast.LENGTH_SHORT)
                        .show();
            } else {
                mAlertDialog = new AlertDialog.Builder(mContext)
                        .setTitle(R.string.reset_esim_error_title)
                        .setMessage(R.string.reset_esim_error_msg)
                        .setPositiveButton(android.R.string.ok, null /* listener */)
                        .show();
            }
        }
    }

    /**
     * The user has gone through the multiple confirmation, so now we go ahead
     * and reset the network settings to its factory-default state.
     */
    @VisibleForTesting
    Button.OnClickListener mFinalClickListener = new Button.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (Utils.isMonkeyRunning()) {
                return;
            }

            // abandon execution if subscription no longer active
            Integer subId = mResetSubscriptionContract.getAnyMissingSubscriptionId();
            if (subId != null) {
                Log.w(TAG, "subId " + subId + " no longer active");
                getActivity().onBackPressed();
                return;
            }

            // Should dismiss the progress dialog firstly if it is showing
            // Or not the progress dialog maybe not dismissed in fast clicking.
            if (mProgressDialog != null && mProgressDialog.isShowing()) {
                mProgressDialog.dismiss();
            }

            mProgressDialog = getProgressDialog(mActivity);
            mProgressDialog.show();

            mResetNetworkTask = new ResetNetworkTask(mActivity);
            mResetNetworkTask.execute();
        }
    };

    private ProgressDialog getProgressDialog(Context context) {
        final ProgressDialog progressDialog = new ProgressDialog(context);
        progressDialog.setIndeterminate(true);
        progressDialog.setCancelable(false);
        progressDialog.setMessage(
                context.getString(R.string.main_clear_progress_text));
        return progressDialog;
    }

    /**
     * Configure the UI for the final confirmation interaction
     */
    private void establishFinalConfirmationState() {
        mContentView.findViewById(R.id.execute_reset_network)
                .setOnClickListener(mFinalClickListener);
    }

    @VisibleForTesting
    void setSubtitle() {
        if (mResetNetworkRequest.getResetEsimPackageName() != null) {
            ((TextView) mContentView.findViewById(R.id.reset_network_confirm))
                    .setText(R.string.reset_network_final_desc_esim);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = (new ResetNetworkRestrictionViewBuilder(mActivity)).build();
        if (view != null) {
            mResetSubscriptionContract.close();
            Log.w(TAG, "Access deny.");
            return view;
        }
        mContentView = inflater.inflate(R.layout.reset_network_confirm, null);
        establishFinalConfirmationState();
        setSubtitle();
        return mContentView;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle args = getArguments();
        if (args == null) {
            args = savedInstanceState;
        }
        mResetNetworkRequest = new ResetNetworkRequest(args);

        mActivity = getActivity();

        mResetSubscriptionContract = new ResetSubscriptionContract(getContext(),
                mResetNetworkRequest) {
            @Override
            public void onSubscriptionInactive(int subscriptionId) {
                // close UI if subscription no longer active
                Log.w(TAG, "subId " + subscriptionId + " no longer active.");
                getActivity().onBackPressed();
            }
        };
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mResetNetworkRequest.writeIntoBundle(outState);
    }

    @Override
    public void onDestroy() {
        if (mResetNetworkTask != null) {
            mResetNetworkTask.cancel(true /* mayInterruptIfRunning */);
            mResetNetworkTask = null;
        }
        if (mResetSubscriptionContract != null) {
            mResetSubscriptionContract.close();
            mResetSubscriptionContract = null;
        }
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
        }
        if (mAlertDialog != null) {
            mAlertDialog.dismiss();
        }
        super.onDestroy();
    }

    @Override
    public int getMetricsCategory() {
        return SettingsEnums.RESET_NETWORK_CONFIRM;
    }
}
+0 −157
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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;

import android.content.Context;
import android.telephony.SubscriptionManager;
import android.telephony.SubscriptionManager.OnSubscriptionsChangedListener;
import android.util.Log;

import androidx.annotation.VisibleForTesting;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.IntStream;

/**
 * A Class monitoring the availability of subscription IDs provided within reset request.
 *
 * This is to detect the situation when user changing SIM card during the presenting of
 * confirmation UI.
 */
public class ResetSubscriptionContract implements AutoCloseable {
    private static final String TAG = "ResetSubscriptionContract";

    private final Context mContext;
    private ExecutorService mExecutorService;
    private final int [] mResetSubscriptionIds;
    @VisibleForTesting
    protected OnSubscriptionsChangedListener mSubscriptionsChangedListener;
    private AtomicBoolean mSubscriptionsUpdateNotify = new AtomicBoolean();

    /**
     * Constructor
     * @param context Context
     * @param resetRequest the request object for perform network reset operation.
     */
    public ResetSubscriptionContract(Context context, ResetNetworkRequest resetRequest) {
        mContext = context;
        // Only keeps specific subscription ID required to perform reset operation
        IntStream subIdStream = IntStream.of(
                resetRequest.getResetTelephonyAndNetworkPolicyManager(),
                resetRequest.getResetApnSubId(), resetRequest.getResetImsSubId());
        mResetSubscriptionIds = subIdStream.sorted().distinct()
                .filter(id -> SubscriptionManager.isUsableSubscriptionId(id))
                .toArray();

        if (mResetSubscriptionIds.length <= 0) {
            return;
        }

        // Monitoring callback through background thread
        mExecutorService = Executors.newSingleThreadExecutor();
        startMonitorSubscriptionChange();
    }

    /**
     * A method for detecting if there's any subscription under monitor no longer active.
     * @return subscription ID which is no longer active.
     */
    public Integer getAnyMissingSubscriptionId() {
        if (mResetSubscriptionIds.length <= 0) {
            return null;
        }
        SubscriptionManager mgr = getSubscriptionManager();
        if (mgr == null) {
            Log.w(TAG, "Fail to access subscription manager");
            return mResetSubscriptionIds[0];
        }
        for (int idx = 0; idx < mResetSubscriptionIds.length; idx++) {
            int subId = mResetSubscriptionIds[idx];
            if (mgr.getActiveSubscriptionInfo(subId) == null) {
                Log.w(TAG, "SubId " + subId + " no longer active.");
                return subId;
            }
        }
        return null;
    }

    /**
     * Async callback when detecting if there's any subscription under monitor no longer active.
     * @param subscriptionId subscription ID which is no longer active.
     */
    public void onSubscriptionInactive(int subscriptionId) {}

    @VisibleForTesting
    protected SubscriptionManager getSubscriptionManager() {
        return mContext.getSystemService(SubscriptionManager.class);
    }

    @VisibleForTesting
    protected OnSubscriptionsChangedListener getChangeListener() {
        return new OnSubscriptionsChangedListener() {
            @Override
            public void onSubscriptionsChanged() {
                /**
                 * Reducing the processing time on main UI thread through a flag.
                 * Once flag get into false, which means latest callback has been
                 * processed.
                 */
                mSubscriptionsUpdateNotify.set(true);

                // Back to main UI thread
                mContext.getMainExecutor().execute(() -> {
                    // Remove notifications and perform checking.
                    if (mSubscriptionsUpdateNotify.getAndSet(false)) {
                        Integer subId = getAnyMissingSubscriptionId();
                        if (subId != null) {
                            onSubscriptionInactive(subId);
                        }
                    }
                });
            }
        };
    }

    private void startMonitorSubscriptionChange() {
        SubscriptionManager mgr = getSubscriptionManager();
        if (mgr == null) {
            return;
        }
        // update monitor listener
        mSubscriptionsChangedListener = getChangeListener();

        mgr.addOnSubscriptionsChangedListener(
                mExecutorService, mSubscriptionsChangedListener);
    }

    // Implementation of AutoCloseable
    public void close() {
        if (mExecutorService == null) {
            return;
        }
        // Stop monitoring subscription change
        SubscriptionManager mgr = getSubscriptionManager();
        if (mgr != null) {
            mgr.removeOnSubscriptionsChangedListener(mSubscriptionsChangedListener);
        }
        // Release Executor
        mExecutorService.shutdownNow();
        mExecutorService = null;
    }
}
+15 −12
Original line number Diff line number Diff line
@@ -65,6 +65,8 @@ public class ResetNetworkOperationBuilder {

    private Context mContext;
    private List<Runnable> mResetSequence = new ArrayList<Runnable>();
    @Nullable
    private Consumer<Boolean> mResetEsimResultCallback = null;

    /**
     * Constructor of builder.
@@ -129,31 +131,32 @@ public class ResetNetworkOperationBuilder {
    }

    /**
     * Append a step of resetting E-SIM.
     * @param callerPackage package name of caller
     * Append a result callback of resetting E-SIM.
     * @param resultCallback a callback dealing with result of resetting eSIM
     * @return this
     */
    public ResetNetworkOperationBuilder resetEsim(String callerPackage) {
        resetEsim(callerPackage, null);
    public ResetNetworkOperationBuilder resetEsimResultCallback(Consumer<Boolean> resultCallback) {
        mResetEsimResultCallback = resultCallback;
        return this;
    }

    /**
     * Append a step of resetting E-SIM.
     * @param callerPackage package name of caller
     * @param resultCallback a Consumer<Boolean> dealing with result of resetting eSIM
     * @return this
     */
    public ResetNetworkOperationBuilder resetEsim(String callerPackage,
            Consumer<Boolean> resultCallback) {
    public ResetNetworkOperationBuilder resetEsim(String callerPackage) {
        Runnable runnable = () -> {
            long startTime = SystemClock.elapsedRealtime();

            if (!DRY_RUN) {
                Boolean wipped = RecoverySystem.wipeEuiccData(mContext, callerPackage);
                if (resultCallback != null) {
                    resultCallback.accept(wipped);
            boolean wipped;
            if (DRY_RUN) {
                wipped = true;
            } else {
                wipped = RecoverySystem.wipeEuiccData(mContext, callerPackage);
            }
            if (mResetEsimResultCallback != null) {
                mResetEsimResultCallback.accept(wipped);
            }

            long endTime = SystemClock.elapsedRealtime();
+9 −6
Original line number Diff line number Diff line
@@ -25,14 +25,17 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.onEach

@OptIn(ExperimentalCoroutinesApi::class)
class CallStateRepository(private val context: Context) {
    private val subscriptionManager = context.requireSubscriptionManager()
class CallStateRepository(
    private val context: Context,
    private val subscriptionRepository: SubscriptionRepository = SubscriptionRepository(context),
) {

    /** Flow for call state of given [subId]. */
    fun callStateFlow(subId: Int): Flow<Int> = context.telephonyCallbackFlow(subId) {
@@ -48,9 +51,8 @@ class CallStateRepository(private val context: Context) {
     *
     * @return true if any active subscription's call state is not idle.
     */
    fun isInCallFlow(): Flow<Boolean> = context.subscriptionsChangedFlow()
        .flatMapLatest {
            val subIds = subscriptionManager.activeSubscriptionIdList
    fun isInCallFlow(): Flow<Boolean> = subscriptionRepository.activeSubscriptionIdListFlow()
        .flatMapLatest { subIds ->
            if (subIds.isEmpty()) {
                flowOf(false)
            } else {
@@ -59,9 +61,10 @@ class CallStateRepository(private val context: Context) {
                }
            }
        }
        .distinctUntilChanged()
        .conflate()
        .flowOn(Dispatchers.Default)
        .onEach { Log.d(TAG, "isInCallFlow: $it") }
        .flowOn(Dispatchers.Default)

    private companion object {
        private const val TAG = "CallStateRepository"
Loading