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

Commit c16e7fa4 authored by Chiachang Wang's avatar Chiachang Wang
Browse files

Use alternative PhoneStateListener formal API

ConnectivityService is going to become a mainline module which
will not able to access hidden APIs. Replace with alternative
constructor with an executor.

Bug: 171183530
Test: atest FrameworksNetTests
Test: manually test to check if phone states notify correctly
Change-Id: Iebd1f45384570bb9ba6be8041dd17310f7b025d3
parent bfceacea
Loading
Loading
Loading
Loading
+23 −4
Original line number Diff line number Diff line
@@ -19,12 +19,12 @@ package com.android.server.connectivity;
import static android.telephony.AccessNetworkConstants.TRANSPORT_TYPE_WWAN;
import static android.telephony.NetworkRegistrationInfo.DOMAIN_PS;

import android.annotation.NonNull;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Looper;
import android.os.RemoteException;
import android.telephony.NetworkRegistrationInfo;
import android.telephony.PhoneStateListener;
@@ -36,6 +36,9 @@ import android.util.Log;
import com.android.internal.app.IBatteryStats;
import com.android.server.am.BatteryStatsService;

import java.util.concurrent.Executor;
import java.util.concurrent.RejectedExecutionException;

public class DataConnectionStats extends BroadcastReceiver {
    private static final String TAG = "DataConnectionStats";
    private static final boolean DEBUG = false;
@@ -55,7 +58,8 @@ public class DataConnectionStats extends BroadcastReceiver {
        mContext = context;
        mBatteryStats = BatteryStatsService.getService();
        mListenerHandler = listenerHandler;
        mPhoneStateListener = new PhoneStateListenerImpl(listenerHandler.getLooper());
        mPhoneStateListener =
                new PhoneStateListenerImpl(new PhoneStateListenerExecutor(listenerHandler));
    }

    public void startMonitoring() {
@@ -140,9 +144,24 @@ public class DataConnectionStats extends BroadcastReceiver {
                && mServiceState.getState() != ServiceState.STATE_POWER_OFF;
    }

    private static class PhoneStateListenerExecutor implements Executor {
        @NonNull
        private final Handler mHandler;

        PhoneStateListenerExecutor(@NonNull Handler handler) {
            mHandler = handler;
        }
        @Override
        public void execute(Runnable command) {
            if (!mHandler.post(command)) {
                throw new RejectedExecutionException(mHandler + " is shutting down");
            }
        }
    }

    private class PhoneStateListenerImpl extends PhoneStateListener {
        PhoneStateListenerImpl(Looper looper) {
            super(looper);
        PhoneStateListenerImpl(Executor executor) {
            super(executor);
        }

        @Override