Merge "Add test cases for isOnlyNonTerrestrialNetwork() methoid" into main
diff --git a/testapps/TestSatelliteApp/AndroidManifest.xml b/testapps/TestSatelliteApp/AndroidManifest.xml
index 4e9a7c1..fb30bf3 100644
--- a/testapps/TestSatelliteApp/AndroidManifest.xml
+++ b/testapps/TestSatelliteApp/AndroidManifest.xml
@@ -19,6 +19,7 @@
     package="com.android.phone.testapps.satellitetestapp">
     <uses-permission android:name="android.permission.BIND_SATELLITE_SERVICE"/>
     <uses-permission android:name="android.permission.SATELLITE_COMMUNICATION"/>
+    <uses-permission android:name="android.permission.READ_PRIVILEGED_PHONE_STATE"/>
     <application android:label="SatelliteTestApp">
         <activity android:name=".SatelliteTestApp"
              android:label="SatelliteTestApp"
diff --git a/testapps/TestSatelliteApp/res/layout/activity_TestSatelliteWrapper.xml b/testapps/TestSatelliteApp/res/layout/activity_TestSatelliteWrapper.xml
index 9035c0c..3365bb9 100644
--- a/testapps/TestSatelliteApp/res/layout/activity_TestSatelliteWrapper.xml
+++ b/testapps/TestSatelliteApp/res/layout/activity_TestSatelliteWrapper.xml
@@ -53,23 +53,46 @@
             android:layout_height="wrap_content"
             android:paddingRight="4dp"
             android:text="@string/unregisterForNtnSignalStrengthChanged"/>
-         <Button
-            android:id="@+id/Back"
-            android:onClick="Back"
-            android:textColor="@android:color/holo_blue_dark"
-            android:layout_marginTop="100dp"
-            android:layout_gravity="center"
-            android:layout_width="wrap_content"
+        <Button
+            android:id="@+id/isOnlyNonTerrestrialNetworkSubscription"
+            android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:paddingRight="4dp"
-            android:text="@string/Back"/>
-         <TextView
-            android:id="@+id/text_id"
-            android:layout_width="300dp"
-            android:layout_height="200dp"
+            android:text="@string/isOnlyNonTerrestrialNetworkSubscription"/>
+        <LinearLayout
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:orientation="horizontal">
+             <Button
+                android:id="@+id/Back"
+                android:onClick="Back"
+                android:textColor="@android:color/holo_blue_dark"
+                android:layout_marginTop="10dp"
+                android:layout_marginBottom="10dp"
+                android:layout_width="0dp"
+                android:layout_height="wrap_content"
+                android:layout_weight="1"
+                android:paddingRight="4dp"
+                android:text="@string/Back"/>
+            <Button
+                android:id="@+id/ClearLog"
+                android:onClick="ClearLog"
+                android:textColor="@android:color/holo_blue_dark"
+                android:layout_marginTop="10dp"
+                android:layout_marginBottom="10dp"
+                android:layout_width="0dp"
+                android:layout_height="wrap_content"
+                android:layout_weight="1"
+                android:paddingRight="4dp"
+                android:text="@string/ClearLog"/>
+        </LinearLayout>
+        <ListView
+            android:id="@+id/logListView"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
             android:capitalize="characters"
             android:textColor="@android:color/holo_blue_light"
             android:layout_centerVertical="true"
-            android:textSize="15dp" />
+            android:textSize="8dp" />
     </LinearLayout>
 </LinearLayout>
diff --git a/testapps/TestSatelliteApp/res/layout/log_textview.xml b/testapps/TestSatelliteApp/res/layout/log_textview.xml
new file mode 100644
index 0000000..a44641b
--- /dev/null
+++ b/testapps/TestSatelliteApp/res/layout/log_textview.xml
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ 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.
+  -->
+
+<TextView xmlns:android="http://schemas.android.com/apk/res/android"
+    android:id="@+id/log_textview"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:textColor="@android:color/holo_blue_light"
+    android:padding="10dp"/>
diff --git a/testapps/TestSatelliteApp/res/values/donottranslate_strings.xml b/testapps/TestSatelliteApp/res/values/donottranslate_strings.xml
index b39ba59..5ab2475 100644
--- a/testapps/TestSatelliteApp/res/values/donottranslate_strings.xml
+++ b/testapps/TestSatelliteApp/res/values/donottranslate_strings.xml
@@ -61,6 +61,8 @@
     <string name="requestNtnSignalStrength">requestNtnSignalStrength</string>
     <string name="registerForNtnSignalStrengthChanged">registerForNtnSignalStrengthChanged</string>
     <string name="unregisterForNtnSignalStrengthChanged">unregisterForNtnSignalStrengthChanged</string>
+    <string name="isOnlyNonTerrestrialNetworkSubscription">isOnlyNonTerrestrialNetworkSubscription</string>
 
     <string name="Back">Back</string>
+    <string name="ClearLog">Clear Log</string>
 </resources>
diff --git a/testapps/TestSatelliteApp/src/com/android/phone/testapps/satellitetestapp/TestSatelliteWrapper.java b/testapps/TestSatelliteApp/src/com/android/phone/testapps/satellitetestapp/TestSatelliteWrapper.java
index 16df37e..13b31f3 100644
--- a/testapps/TestSatelliteApp/src/com/android/phone/testapps/satellitetestapp/TestSatelliteWrapper.java
+++ b/testapps/TestSatelliteApp/src/com/android/phone/testapps/satellitetestapp/TestSatelliteWrapper.java
@@ -21,32 +21,44 @@
 import android.content.Intent;
 import android.os.Bundle;
 import android.os.OutcomeReceiver;
+import android.telephony.SubscriptionInfo;
+import android.telephony.SubscriptionManager;
 import android.telephony.satellite.wrapper.NtnSignalStrengthCallbackWrapper;
 import android.telephony.satellite.wrapper.NtnSignalStrengthWrapper;
 import android.telephony.satellite.wrapper.SatelliteManagerWrapper;
 import android.util.Log;
 import android.view.View;
 import android.view.View.OnClickListener;
-import android.widget.TextView;
-import android.widget.Toast;
+import android.widget.ArrayAdapter;
+import android.widget.ListView;
 
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
+import java.util.stream.Collectors;
+
 
 /**
  * Activity related to SatelliteControl APIs for satellite.
  */
 public class TestSatelliteWrapper extends Activity {
     private static final String TAG = "TestSatelliteWrapper";
+    ArrayList<String> mLogMessages = new ArrayList<>();
+    ArrayAdapter<String> mAdapter;
 
     private final ExecutorService mExecutor = Executors.newSingleThreadExecutor();
     private SatelliteManagerWrapper mSatelliteManagerWrapper;
     private NtnSignalStrengthCallback mNtnSignalStrengthCallback = null;
+    private SubscriptionManager mSubscriptionManager;
+    private ListView mLogListView;
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         mSatelliteManagerWrapper = SatelliteManagerWrapper.getInstance(this);
+        mSubscriptionManager = getSystemService(SubscriptionManager.class);
 
         setContentView(R.layout.activity_TestSatelliteWrapper);
         findViewById(R.id.requestNtnSignalStrength)
@@ -55,12 +67,32 @@
                 .setOnClickListener(this::registerForNtnSignalStrengthChanged);
         findViewById(R.id.unregisterForNtnSignalStrengthChanged)
                 .setOnClickListener(this::unregisterForNtnSignalStrengthChanged);
+        findViewById(R.id.isOnlyNonTerrestrialNetworkSubscription)
+                .setOnClickListener(this::isOnlyNonTerrestrialNetworkSubscription);
         findViewById(R.id.Back).setOnClickListener(new OnClickListener() {
             @Override
             public void onClick(View view) {
                 startActivity(new Intent(TestSatelliteWrapper.this, SatelliteTestApp.class));
             }
         });
+        findViewById(R.id.ClearLog).setOnClickListener(new OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                clearListView();
+            }
+        });
+
+        mLogListView = findViewById(R.id.logListView);
+        mAdapter = new ArrayAdapter<>(this, R.layout.log_textview, mLogMessages);
+        mLogListView.setAdapter(mAdapter);
+
+        addLogMessage("TestSatelliteWrapper.onCreate()");
+    }
+
+
+    private void clearListView() {
+        mLogMessages.clear();
+        mAdapter.notifyDataSetChanged();
     }
 
     @Override
@@ -75,17 +107,15 @@
     }
 
     private void requestNtnSignalStrength(View view) {
+        addLogMessage("requestNtnSignalStrength");
         Log.d(TAG, "requestNtnSignalStrength");
-        TextView textView = findViewById(R.id.text_id);
         OutcomeReceiver<NtnSignalStrengthWrapper,
                 SatelliteManagerWrapper.SatelliteExceptionWrapper> receiver =
                 new OutcomeReceiver<>() {
                     @Override
                     public void onResult(NtnSignalStrengthWrapper level) {
-                        TextView textView = findViewById(R.id.text_id);
                         if (level != null) {
-                            textView.setText("requestNtnSignalStrength level is "
-                                    + level.getLevel());
+                            addLogMessage("requestNtnSignalStrength level is " + level.getLevel());
                         }
                     }
 
@@ -93,11 +123,10 @@
                     public void onError(
                             SatelliteManagerWrapper.SatelliteExceptionWrapper exception) {
                         if (exception != null) {
-                            TextView textView = findViewById(R.id.text_id);
                             String onError = "requestNtnSignalStrength exception: "
                                     + translateResultCodeToString(exception.getErrorCode());
                             Log.d(TAG, onError);
-                            textView.setText(onError);
+                            addLogMessage(onError);
                         }
                     }
                 };
@@ -106,8 +135,8 @@
     }
 
     private void registerForNtnSignalStrengthChanged(View view) {
+        addLogMessage("registerForNtnSignalStrengthChanged");
         Log.d(TAG, "registerForNtnSignalStrengthChanged()");
-        TextView textView = findViewById(R.id.text_id);
         if (mNtnSignalStrengthCallback == null) {
             Log.d(TAG, "create new NtnSignalStrengthCallback instance.");
             mNtnSignalStrengthCallback = new NtnSignalStrengthCallback();
@@ -117,21 +146,48 @@
         if (result != SatelliteManagerWrapper.SATELLITE_RESULT_SUCCESS) {
             String onError = translateResultCodeToString(result);
             Log.d(TAG, onError);
-            textView.setText(onError);
+            addLogMessage(onError);
             mNtnSignalStrengthCallback = null;
         }
     }
 
     private void unregisterForNtnSignalStrengthChanged(View view) {
+        addLogMessage("unregisterForNtnSignalStrengthChanged");
         Log.d(TAG, "unregisterForNtnSignalStrengthChanged()");
-        TextView textView = findViewById(R.id.text_id);
         if (mNtnSignalStrengthCallback != null) {
             mSatelliteManagerWrapper.unregisterForNtnSignalStrengthChanged(
                     mNtnSignalStrengthCallback);
             mNtnSignalStrengthCallback = null;
-            textView.setText("mNtnSignalStrengthCallback was unregistered");
+            addLogMessage("mNtnSignalStrengthCallback was unregistered");
         } else {
-            textView.setText("mNtnSignalStrengthCallback is null, ignored.");
+            addLogMessage("mNtnSignalStrengthCallback is null, ignored.");
+        }
+    }
+
+    private void isOnlyNonTerrestrialNetworkSubscription(View view) {
+        addLogMessage("isOnlyNonTerrestrialNetworkSubscription");
+        Log.d(TAG, "isOnlyNonTerrestrialNetworkSubscription()");
+        List<SubscriptionInfo> infoList = mSubscriptionManager.getAvailableSubscriptionInfoList();
+        List<Integer> subIdList = infoList.stream()
+                .map(SubscriptionInfo::getSubscriptionId)
+                .toList();
+
+        Map<Integer, Boolean> resultMap = subIdList.stream().collect(
+                Collectors.toMap(
+                        id -> id,
+                        id -> {
+                            boolean result = mSatelliteManagerWrapper
+                                    .isOnlyNonTerrestrialNetworkSubscription(id);
+                            addLogMessage("SatelliteManagerWrapper"
+                                    + ".isOnlyNonTerrestrialNetworkSubscription(" + id + ")");
+                            return result;
+                        }
+                ));
+
+        for (Map.Entry<Integer, Boolean> entry : resultMap.entrySet()) {
+            int subId = entry.getKey();
+            boolean result = entry.getValue();
+            addLogMessage("Subscription ID: " + subId + ", Result: " + result);
         }
     }
 
@@ -139,15 +195,9 @@
         @Override
         public void onNtnSignalStrengthChanged(
                 @NonNull NtnSignalStrengthWrapper ntnSignalStrength) {
-            String toastMessage = "Received NTN SignalStrength : " + ntnSignalStrength.getLevel();
-            Log.d(TAG, toastMessage);
-            runOnUiThread(new Runnable() {
-                @Override
-                public void run() {
-                    Toast.makeText(getApplicationContext(), toastMessage,
-                            Toast.LENGTH_SHORT).show();
-                }
-            });
+            String message = "Received NTN SignalStrength : " + ntnSignalStrength.getLevel();
+            Log.d(TAG, message);
+            runOnUiThread(() -> addLogMessage(message));
         }
     }
 
@@ -204,4 +254,10 @@
                 return "INVALID CODE: " + result;
         }
     }
+
+    private void addLogMessage(String message) {
+        mLogMessages.add(message);
+        mAdapter.notifyDataSetChanged();
+        mLogListView.setSelection(mAdapter.getCount() - 1);
+    }
 }