• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.phone.testapps.satellitetestapp;
18 
19 import android.app.Activity;
20 import android.content.Intent;
21 import android.content.SharedPreferences;
22 import android.os.Bundle;
23 import android.os.CancellationSignal;
24 import android.os.OutcomeReceiver;
25 import android.telephony.satellite.SatelliteManager;
26 import android.telephony.satellite.SatelliteProvisionStateCallback;
27 import android.telephony.satellite.SatelliteSubscriberProvisionStatus;
28 import android.telephony.satellite.stub.SatelliteResult;
29 import android.util.Log;
30 import android.view.View;
31 import android.view.View.OnClickListener;
32 import android.widget.TextView;
33 
34 import java.util.List;
35 import java.util.concurrent.LinkedBlockingQueue;
36 import java.util.concurrent.TimeUnit;
37 import java.util.concurrent.atomic.AtomicReference;
38 
39 /**
40  * Activity related to Provisioning APIs.
41  */
42 public class Provisioning extends Activity {
43 
44     private static final String TAG = "SatelliteProvisioning";
45 
46     private boolean mProvisioned = false;
47 
48     private SatelliteManager mSatelliteManager;
49     private SatelliteProvisionStateCallbackTestApp mCallback;
50     private static final long TIMEOUT = 3000;
51 
52     @Override
onCreate(Bundle savedInstanceState)53     public void onCreate(Bundle savedInstanceState) {
54         super.onCreate(savedInstanceState);
55         mSatelliteManager = getSystemService(SatelliteManager.class);
56         mCallback = new SatelliteProvisionStateCallbackTestApp();
57 
58         setContentView(R.layout.activity_Provisioning);
59         findViewById(R.id.provisionSatelliteService)
60                 .setOnClickListener(this::provisionServiceApp);
61         findViewById(R.id.deprovisionSatelliteService)
62                 .setOnClickListener(this::deprovisionServiceApp);
63         findViewById(R.id.requestIsSatelliteProvisioned)
64                 .setOnClickListener(this::requestIsProvisionedApp);
65         findViewById(R.id.registerForSatelliteProvisionStateChanged)
66                 .setOnClickListener(this::registerForProvisionStateChangedApp);
67         findViewById(R.id.unregisterForSatelliteProvisionStateChanged)
68                 .setOnClickListener(this::unregisterForProvisionStateChangedApp);
69         findViewById(R.id.showCurrentSatelliteProvisionState)
70                 .setOnClickListener(this::showCurrentSatelliteProvisionStateApp);
71         findViewById(R.id.Back).setOnClickListener(new OnClickListener() {
72             @Override
73             public void onClick(View view) {
74                 startActivity(new Intent(Provisioning.this, SatelliteTestApp.class));
75             }
76         });
77     }
78 
79     protected class SatelliteProvisionStateCallbackTestApp implements
80             SatelliteProvisionStateCallback {
81         @Override
onSatelliteProvisionStateChanged(boolean provisioned)82         public void onSatelliteProvisionStateChanged(boolean provisioned) {
83             mProvisioned = provisioned;
84             Log.d(TAG, "onSatelliteProvisionStateChanged in SatelliteTestApp: provisioned="
85                     + mProvisioned);
86         }
87 
88         @Override
onSatelliteSubscriptionProvisionStateChanged( List<SatelliteSubscriberProvisionStatus> list)89         public void onSatelliteSubscriptionProvisionStateChanged(
90                 List<SatelliteSubscriberProvisionStatus> list) {
91             Log.d(TAG, "onSatelliteSubscriptionProvisionStateChanged in SatelliteTestApp" + list);
92         }
93     }
94 
provisionServiceApp(View view)95     private void provisionServiceApp(View view) {
96         TextView textView = findViewById(R.id.text_id);
97         CancellationSignal cancellationSignal = new CancellationSignal();
98         LinkedBlockingQueue<Integer> error = new LinkedBlockingQueue<>(1);
99         String mText = "This is test provision data.";
100         byte[] testProvisionData = mText.getBytes();
101         mSatelliteManager.provisionService("SATELLITE_TOKEN", testProvisionData,
102                 cancellationSignal, Runnable::run, error::offer);
103         try {
104             Integer value = error.poll(TIMEOUT, TimeUnit.MILLISECONDS);
105             if (value == null) {
106                 textView.setText("Timed out to provision the satellite");
107             } else if (value != SatelliteResult.SATELLITE_RESULT_SUCCESS) {
108                 textView.setText("Failed to provision SatelliteService with error = "
109                         + SatelliteErrorUtils.mapError(value));
110             } else {
111                 textView.setText("Successfully provisioned the satellite");
112             }
113         } catch (InterruptedException e) {
114             textView.setText("Provision SatelliteService exception caught =" + e);
115         }
116     }
117 
deprovisionServiceApp(View view)118     private void deprovisionServiceApp(View view) {
119         TextView textView = findViewById(R.id.text_id);
120         LinkedBlockingQueue<Integer> error = new LinkedBlockingQueue<>(1);
121         mSatelliteManager.deprovisionService("SATELLITE_TOKEN", Runnable::run,
122                 error::offer);
123         try {
124             Integer value = error.poll(TIMEOUT, TimeUnit.MILLISECONDS);
125             if (value == null) {
126                 textView.setText("Timed out to deprovision the satellite");
127             } else if (value != SatelliteResult.SATELLITE_RESULT_SUCCESS) {
128                 textView.setText("Failed to deprovision SatelliteService with error = "
129                         + SatelliteErrorUtils.mapError(value));
130             } else {
131                 textView.setText("Successfully deprovisioned the satellite");
132             }
133         } catch (InterruptedException e) {
134             textView.setText("Deprovision SatelliteService exception caught =" + e);
135         }
136     }
137 
requestIsProvisionedApp(View view)138     private void requestIsProvisionedApp(View view) {
139         final AtomicReference<Boolean> enabled = new AtomicReference<>();
140         final AtomicReference<Integer> errorCode = new AtomicReference<>();
141         OutcomeReceiver<Boolean, SatelliteManager.SatelliteException> mReceiver =
142                 new OutcomeReceiver<>() {
143             @Override
144             public void onResult(Boolean result) {
145                 enabled.set(result);
146                 TextView textView = findViewById(R.id.text_id);
147                 textView.setText("Status for requestIsSatelliteProvisioned result: "
148                         + enabled.get());
149             }
150 
151             @Override
152             public void onError(SatelliteManager.SatelliteException exception) {
153                 errorCode.set(exception.getErrorCode());
154                 TextView textView = findViewById(R.id.text_id);
155                 textView.setText("Status for requestIsSatelliteProvisioned error : "
156                         + SatelliteErrorUtils.mapError(errorCode.get()));
157             }
158         };
159         mSatelliteManager.requestIsProvisioned(Runnable::run, mReceiver);
160     }
161 
registerForProvisionStateChangedApp(View view)162     private void registerForProvisionStateChangedApp(View view) {
163         int result = mSatelliteManager.registerForProvisionStateChanged(Runnable::run,
164                 mCallback);
165         TextView textView = findViewById(R.id.text_id);
166         textView.setText("Status for registerForSatelliteProvisionStateChanged : "
167                 + SatelliteErrorUtils.mapError(result));
168     }
169 
unregisterForProvisionStateChangedApp(View view)170     private void unregisterForProvisionStateChangedApp(View view) {
171         mSatelliteManager.unregisterForProvisionStateChanged(mCallback);
172         TextView textView = findViewById(R.id.text_id);
173         textView.setText("unregisterForSatelliteProvisionStateChanged is successful");
174     }
175 
showCurrentSatelliteProvisionStateApp(View view)176     private void showCurrentSatelliteProvisionStateApp(View view) {
177         TextView textView = findViewById(R.id.text_id);
178         textView.setText("Current Provision State is " + mProvisioned);
179     }
180 
181     // Fetch the stored data in onResume()
182     // Because this is what will be called when the app opens again
183     @Override
onResume()184     protected void onResume() {
185         super.onResume();
186         // Fetching the stored data from the SharedPreference
187         SharedPreferences sh = getSharedPreferences("MySharedPref", MODE_PRIVATE);
188         boolean isProvisioned = sh.getBoolean("provision_state", mProvisioned);
189 
190         // Setting the fetched data
191         mProvisioned = isProvisioned;
192     }
193 
194     // Store the data in the SharedPreference in the onPause() method
195     // When the user closes the application onPause() will be called and data will be stored
196     @Override
onPause()197     protected void onPause() {
198         super.onPause();
199         // Creating a shared pref object with a file name "MySharedPref" in private mode
200         SharedPreferences sharedPreferences = getSharedPreferences("MySharedPref", MODE_PRIVATE);
201         SharedPreferences.Editor myEdit = sharedPreferences.edit();
202 
203         // write all the data entered by the user in SharedPreference and apply
204         myEdit.putBoolean("provision_state", mProvisioned);
205         myEdit.apply();
206     }
207 
onDestroy()208     protected void onDestroy() {
209         super.onDestroy();
210         SharedPreferences sharedPreferences = getSharedPreferences("MySharedPref", MODE_PRIVATE);
211 
212         final SharedPreferences.Editor sharedPrefsEditor = sharedPreferences.edit();
213         sharedPrefsEditor.remove("provision_state");
214         sharedPrefsEditor.commit();
215     }
216 }
217