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

Commit d292c155 authored by Sudheer Shanka's avatar Sudheer Shanka Committed by Android (Google) Code Review
Browse files

Merge "Update AMS to use NetworkManagementInternal.isNetworkRestrictedForUid."

parents c9d150dc 5918c670
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -9757,6 +9757,15 @@ public final class Settings {
         */
        public static final String RETAIL_DEMO_MODE_CONSTANTS = "retail_demo_mode_constants";

        /**
         * Indicates the maximum time that an app is blocked for the network rules to get updated.
         *
         * Type: long
         *
         * @hide
         */
        public static final String NETWORK_ACCESS_TIMEOUT_MS = "network_access_timeout_ms";

        /**
         * The reason for the settings database being downgraded. This is only for
         * troubleshooting purposes and its value should not be interpreted in any way.
+1 −0
Original line number Diff line number Diff line
@@ -328,6 +328,7 @@ public class SettingsBackupTest {
                    Settings.Global.USE_GOOGLE_MAIL,
                    Settings.Global.VT_IMS_ENABLED,
                    Settings.Global.WAIT_FOR_DEBUGGER,
                    Settings.Global.NETWORK_ACCESS_TIMEOUT_MS,
                    Settings.Global.WARNING_TEMPERATURE,
                    Settings.Global.WEBVIEW_DATA_REDUCTION_PROXY_KEY,
                    Settings.Global.WEBVIEW_FALLBACK_LOGIC_ENABLED,
+34 −5
Original line number Diff line number Diff line
@@ -54,6 +54,7 @@ import static android.provider.Settings.Global.DEBUG_APP;
import static android.provider.Settings.Global.DEVELOPMENT_ENABLE_FREEFORM_WINDOWS_SUPPORT;
import static android.provider.Settings.Global.DEVELOPMENT_FORCE_RESIZABLE_ACTIVITIES;
import static android.provider.Settings.Global.DEVELOPMENT_FORCE_RTL;
import static android.provider.Settings.Global.NETWORK_ACCESS_TIMEOUT_MS;
import static android.provider.Settings.Global.WAIT_FOR_DEBUGGER;
import static android.provider.Settings.System.FONT_SCALE;
import static android.service.voice.VoiceInteractionSession.SHOW_SOURCE_APPLICATION;
@@ -357,6 +358,7 @@ import com.android.server.DeviceIdleController;
import com.android.server.IntentResolver;
import com.android.server.LocalServices;
import com.android.server.LockGuard;
import com.android.server.NetworkManagementInternal;
import com.android.server.RescueParty;
import com.android.server.ServiceThread;
import com.android.server.SystemConfig;
@@ -572,9 +574,9 @@ public class ActivityManagerService extends IActivityManager.Stub
    static final boolean TAKE_FULLSCREEN_SCREENSHOTS = true;
    /**
     * Indicates the maximum time spent waiting for the network rules to get updated.
     * Default value for {@link Settings.Global#NETWORK_ACCESS_TIMEOUT_MS}.
     */
    private static final long WAIT_FOR_NETWORK_TIMEOUT_MS = 2000; // 2 sec
    private static final long NETWORK_ACCESS_TIMEOUT_DEFAULT_MS = 0; // 0 sec
    /**
     * State indicating that there is no need for any blocking for network.
@@ -753,6 +755,12 @@ public class ActivityManagerService extends IActivityManager.Stub
    final AppErrors mAppErrors;
    /**
     * Indicates the maximum time spent waiting for the network rules to get updated.
     */
    @VisibleForTesting
    long mWaitForNetworkTimeoutMs;
    public boolean canShowErrorDialogs() {
        return mShowDialogs && !mSleeping && !mShuttingDown
                && !mKeyguardController.isKeyguardShowing();
@@ -13808,6 +13816,8 @@ public class ActivityManagerService extends IActivityManager.Stub
        final boolean forceRtl = Settings.Global.getInt(resolver, DEVELOPMENT_FORCE_RTL, 0) != 0;
        final boolean forceResizable = Settings.Global.getInt(
                resolver, DEVELOPMENT_FORCE_RESIZABLE_ACTIVITIES, 0) != 0;
        final long waitForNetworkTimeoutMs = Settings.Global.getLong(resolver,
                NETWORK_ACCESS_TIMEOUT_MS, NETWORK_ACCESS_TIMEOUT_DEFAULT_MS);
        final boolean supportsLeanbackOnly =
                mContext.getPackageManager().hasSystemFeature(FEATURE_LEANBACK_ONLY);
@@ -13863,6 +13873,7 @@ public class ActivityManagerService extends IActivityManager.Stub
                mFullscreenThumbnailScale = res.getFraction(
                    com.android.internal.R.fraction.thumbnail_fullscreen_scale, 1, 1);
            }
            mWaitForNetworkTimeoutMs = waitForNetworkTimeoutMs;
        }
    }
@@ -22516,6 +22527,9 @@ public class ActivityManagerService extends IActivityManager.Stub
    @VisibleForTesting
    @GuardedBy("this")
    void incrementProcStateSeqAndNotifyAppsLocked() {
        if (mWaitForNetworkTimeoutMs <= 0) {
            return;
        }
        // Used for identifying which uids need to block for network.
        ArrayList<Integer> blockingUids = null;
        for (int i = mActiveUids.size() - 1; i >= 0; --i) {
@@ -23558,10 +23572,14 @@ public class ActivityManagerService extends IActivityManager.Stub
                }
                final long startTime = SystemClock.uptimeMillis();
                record.waitingForNetwork = true;
                record.lock.wait(WAIT_FOR_NETWORK_TIMEOUT_MS);
                record.lock.wait(mWaitForNetworkTimeoutMs);
                record.waitingForNetwork = false;
                final long totalTime = SystemClock.uptimeMillis() - startTime;
                if (DEBUG_NETWORK ||  totalTime > WAIT_FOR_NETWORK_TIMEOUT_MS / 2) {
                if (totalTime >= mWaitForNetworkTimeoutMs) {
                    Slog.wtf(TAG_NETWORK, "Total time waited for network rules to get updated: "
                            + totalTime + ". Uid: " + callingUid + " procStateSeq: "
                            + procStateSeq);
                } else if (DEBUG_NETWORK ||  totalTime >= mWaitForNetworkTimeoutMs / 2) {
                    Slog.d(TAG_NETWORK, "Total time waited for network rules to get updated: "
                            + totalTime + ". Uid: " + callingUid + " procStateSeq: "
                            + procStateSeq);
@@ -23856,6 +23874,8 @@ public class ActivityManagerService extends IActivityManager.Stub
    @VisibleForTesting
    public static class Injector {
        private NetworkManagementInternal mNmi;
        public AppOpsService getAppOpsService(File file, Handler handler) {
            return new AppOpsService(file, handler);
        }
@@ -23865,8 +23885,17 @@ public class ActivityManagerService extends IActivityManager.Stub
        }
        public boolean isNetworkRestrictedForUid(int uid) {
            // TODO: add implementation
            if (ensureHasNetworkManagementInternal()) {
                return mNmi.isNetworkRestrictedForUid(uid);
            }
            return false;
        }
        private boolean ensureHasNetworkManagementInternal() {
            if (mNmi == null) {
                mNmi = LocalServices.getService(NetworkManagementInternal.class);
            }
            return mNmi != null;
        }
    }
}
+12 −0
Original line number Diff line number Diff line
@@ -132,6 +132,7 @@ public class ActivityManagerServiceTest {
        mHandler = new TestHandler(mHandlerThread.getLooper());
        mInjector = new TestInjector();
        mAms = new ActivityManagerService(mInjector);
        mAms.mWaitForNetworkTimeoutMs = 100;
    }

    @After
@@ -217,6 +218,17 @@ public class ActivityManagerServiceTest {
                44, // exptectedCurProcStateSeq
                -1, // expectedBlockState, -1 to verify there are no interactions with main thread.
                false); // expectNotify

        // Verify when waitForNetworkTimeout is 0, then procStateSeq is not incremented.
        mAms.mWaitForNetworkTimeoutMs = 0;
        mInjector.setNetworkRestrictedForUid(true);
        verifySeqCounterAndInteractions(uidRec,
                PROCESS_STATE_TOP, // prevState
                PROCESS_STATE_IMPORTANT_BACKGROUND, // curState
                44, // expectedGlobalCounter
                44, // exptectedCurProcStateSeq
                -1, // expectedBlockState, -1 to verify there are no interactions with main thread.
                false); // expectNotify
    }

    private void verifySeqCounterAndInteractions(UidRecord uidRec, int prevState, int curState,