Support entitlement check in DSDS

Get resource from preferred SIM's subId.

Bug: 122108346
Bug: 120069528
Test: atest TetherServiceTest
Change-Id: Ia4279a418dfa6cd14942b7f4d2a313156066381b
diff --git a/src/com/android/settings/Utils.java b/src/com/android/settings/Utils.java
index 93a785f..e300a44 100644
--- a/src/com/android/settings/Utils.java
+++ b/src/com/android/settings/Utils.java
@@ -71,6 +71,7 @@
 import android.provider.ContactsContract.Profile;
 import android.provider.ContactsContract.RawContacts;
 import android.provider.Settings;
+import android.telephony.SubscriptionManager;
 import android.telephony.TelephonyManager;
 import android.text.Spannable;
 import android.text.SpannableString;
@@ -980,4 +981,13 @@
         }
         return false;
     }
+
+    /** Get {@link Resources} by subscription id if subscription id is valid. */
+    public static Resources getResourcesForSubId(Context context, int subId) {
+        if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
+            return SubscriptionManager.getResourcesForSubId(context, subId);
+        } else {
+            return context.getResources();
+        }
+    }
 }
diff --git a/src/com/android/settings/TetherProvisioningActivity.java b/src/com/android/settings/network/TetherProvisioningActivity.java
similarity index 89%
rename from src/com/android/settings/TetherProvisioningActivity.java
rename to src/com/android/settings/network/TetherProvisioningActivity.java
index e842db1..b30950e 100644
--- a/src/com/android/settings/TetherProvisioningActivity.java
+++ b/src/com/android/settings/network/TetherProvisioningActivity.java
@@ -14,17 +14,21 @@
  * limitations under the License.
  */
 
-package com.android.settings;
+package com.android.settings.network;
 
 import android.app.Activity;
 import android.content.Intent;
 import android.content.pm.PackageManager;
+import android.content.res.Resources;
 import android.net.ConnectivityManager;
 import android.os.Bundle;
 import android.os.ResultReceiver;
 import android.os.UserHandle;
+import android.telephony.SubscriptionManager;
 import android.util.Log;
 
+import com.android.settings.Utils;
+
 /**
  * Activity which acts as a proxy to the tether provisioning app for sanity checks and permission
  * restrictions. Specifically, the provisioning apps require
@@ -47,7 +51,9 @@
 
         int tetherType = getIntent().getIntExtra(ConnectivityManager.EXTRA_ADD_TETHER_TYPE,
                 ConnectivityManager.TETHERING_INVALID);
-        String[] provisionApp = getResources().getStringArray(
+        final int subId = SubscriptionManager.getDefaultDataSubscriptionId();
+        final Resources res = Utils.getResourcesForSubId(this, subId);
+        final String[] provisionApp = res.getStringArray(
                 com.android.internal.R.array.config_mobile_hotspot_provision_app);
 
         Intent intent = new Intent(Intent.ACTION_MAIN);
diff --git a/src/com/android/settings/wifi/tether/TetherService.java b/src/com/android/settings/wifi/tether/TetherService.java
index 4b39f67..09781a8 100644
--- a/src/com/android/settings/wifi/tether/TetherService.java
+++ b/src/com/android/settings/wifi/tether/TetherService.java
@@ -32,16 +32,20 @@
 import android.content.SharedPreferences;
 import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
+import android.content.res.Resources;
 import android.net.ConnectivityManager;
 import android.os.IBinder;
 import android.os.ResultReceiver;
 import android.os.SystemClock;
+import android.telephony.SubscriptionManager;
 import android.text.TextUtils;
 import android.util.ArrayMap;
 import android.util.Log;
 
 import androidx.annotation.VisibleForTesting;
 
+import com.android.settings.Utils;
+
 import java.util.ArrayList;
 import java.util.List;
 
@@ -65,7 +69,7 @@
 
     private int mCurrentTypeIndex;
     private boolean mInProvisionCheck;
-    private UsageStatsManagerWrapper mUsageManagerWrapper;
+    private TetherServiceWrapper mWrapper;
     private ArrayList<Integer> mCurrentTethers;
     private ArrayMap<Integer, List<ResultReceiver>> mPendingCallbacks;
     private HotspotOffReceiver mHotspotReceiver;
@@ -79,7 +83,7 @@
     public void onCreate() {
         super.onCreate();
         if (DEBUG) Log.d(TAG, "Creating TetherService");
-        String provisionResponse = getResources().getString(
+        String provisionResponse = getResourceForDefaultDataSubId().getString(
                 com.android.internal.R.string.config_mobile_hotspot_provision_response);
         registerReceiver(mReceiver, new IntentFilter(provisionResponse),
                 android.Manifest.permission.CONNECTIVITY_INTERNAL, null);
@@ -91,9 +95,6 @@
         mPendingCallbacks.put(ConnectivityManager.TETHERING_USB, new ArrayList<ResultReceiver>());
         mPendingCallbacks.put(
                 ConnectivityManager.TETHERING_BLUETOOTH, new ArrayList<ResultReceiver>());
-        if (mUsageManagerWrapper == null) {
-            mUsageManagerWrapper = new UsageStatsManagerWrapper(this);
-        }
         mHotspotReceiver = new HotspotOffReceiver(this);
     }
 
@@ -258,7 +259,7 @@
     }
 
     private Intent getProvisionBroadcastIntent(int index) {
-        String provisionAction = getResources().getString(
+        String provisionAction = getResourceForDefaultDataSubId().getString(
                 com.android.internal.R.string.config_mobile_hotspot_provision_app_no_ui);
         Intent intent = new Intent(provisionAction);
         int type = mCurrentTethers.get(index);
@@ -282,7 +283,7 @@
         for (ResolveInfo resolver : resolvers) {
             if (resolver.activityInfo.applicationInfo.isSystemApp()) {
                 String packageName = resolver.activityInfo.packageName;
-                mUsageManagerWrapper.setAppInactive(packageName, false);
+                getTetherServiceWrapper().setAppInactive(packageName, false);
             }
         }
     }
@@ -294,7 +295,7 @@
 
         PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);
         AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
-        int period = getResources().getInteger(
+        int period = getResourceForDefaultDataSubId().getInteger(
                 com.android.internal.R.integer.config_mobile_hotspot_provision_check_period);
         long periodMs = period * MS_PER_HOUR;
         long firstTime = SystemClock.elapsedRealtime() + periodMs;
@@ -347,7 +348,7 @@
         @Override
         public void onReceive(Context context, Intent intent) {
             if (DEBUG) Log.d(TAG, "Got provision result " + intent);
-            String provisionResponse = getResources().getString(
+            String provisionResponse = getResourceForDefaultDataSubId().getString(
                     com.android.internal.R.string.config_mobile_hotspot_provision_response);
 
             if (provisionResponse.equals(intent.getAction())) {
@@ -385,19 +386,27 @@
     };
 
     @VisibleForTesting
-    void setUsageStatsManagerWrapper(UsageStatsManagerWrapper wrapper) {
-        mUsageManagerWrapper = wrapper;
+    void setTetherServiceWrapper(TetherServiceWrapper wrapper) {
+        mWrapper = wrapper;
+    }
+
+    private TetherServiceWrapper getTetherServiceWrapper() {
+        if (mWrapper == null) {
+            mWrapper = new TetherServiceWrapper(this);
+        }
+        return mWrapper;
     }
 
     /**
-     * A static helper class used for tests. UsageStatsManager cannot be mocked out becasue
-     * it's marked final. This class can be mocked out instead.
+     * A static helper class used for tests. UsageStatsManager cannot be mocked out because
+     * it's marked final. Static method SubscriptionManager#getResourcesForSubId also cannot
+     * be mocked. This class can be mocked out instead.
      */
     @VisibleForTesting
-    public static class UsageStatsManagerWrapper {
+    public static class TetherServiceWrapper {
         private final UsageStatsManager mUsageStatsManager;
 
-        UsageStatsManagerWrapper(Context context) {
+        TetherServiceWrapper(Context context) {
             mUsageStatsManager = (UsageStatsManager)
                     context.getSystemService(Context.USAGE_STATS_SERVICE);
         }
@@ -405,5 +414,15 @@
         void setAppInactive(String packageName, boolean isInactive) {
             mUsageStatsManager.setAppInactive(packageName, isInactive);
         }
+
+        int getDefaultDataSubscriptionId() {
+            return SubscriptionManager.getDefaultDataSubscriptionId();
+        }
+    }
+
+    @VisibleForTesting
+    Resources getResourceForDefaultDataSubId() {
+        final int subId = getTetherServiceWrapper().getDefaultDataSubscriptionId();
+        return Utils.getResourcesForSubId(this, subId);
     }
 }