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.stub.SatelliteResult;
28 import android.util.Log;
29 import android.view.View;
30 import android.view.View.OnClickListener;
31 import android.widget.TextView;
32 
33 import java.util.concurrent.LinkedBlockingQueue;
34 import java.util.concurrent.TimeUnit;
35 import java.util.concurrent.atomic.AtomicReference;
36 
37 /**
38  * Activity related to Provisioning APIs.
39  */
40 public class Provisioning extends Activity {
41 
42     private static final String TAG = "Provisioning";
43 
44     private boolean mProvisioned = false;
45 
46     private SatelliteManager mSatelliteManager;
47     private SatelliteProvisionStateCallbackTestApp mCallback;
48     private static final long TIMEOUT = 3000;
49 
50     @Override
onCreate(Bundle savedInstanceState)51     public void onCreate(Bundle savedInstanceState) {
52         super.onCreate(savedInstanceState);
53         mSatelliteManager = getSystemService(SatelliteManager.class);
54         mCallback = new SatelliteProvisionStateCallbackTestApp();
55 
56         setContentView(R.layout.activity_Provisioning);
57         findViewById(R.id.provisionSatelliteService)
58                 .setOnClickListener(this::provisionServiceApp);
59         findViewById(R.id.deprovisionSatelliteService)
60                 .setOnClickListener(this::deprovisionServiceApp);
61         findViewById(R.id.requestIsSatelliteProvisioned)
62                 .setOnClickListener(this::requestIsProvisionedApp);
63         findViewById(R.id.registerForSatelliteProvisionStateChanged)
64                 .setOnClickListener(this::registerForProvisionStateChangedApp);
65         findViewById(R.id.unregisterForSatelliteProvisionStateChanged)
66                 .setOnClickListener(this::unregisterForProvisionStateChangedApp);
67         findViewById(R.id.showCurrentSatelliteProvisionState)
68                 .setOnClickListener(this::showCurrentSatelliteProvisionStateApp);
69         findViewById(R.id.Back).setOnClickListener(new OnClickListener() {
70             @Override
71             public void onClick(View view) {
72                 startActivity(new Intent(Provisioning.this, SatelliteTestApp.class));
73             }
74         });
75     }
76 
77     protected class SatelliteProvisionStateCallbackTestApp implements
78             SatelliteProvisionStateCallback {
79         @Override
onSatelliteProvisionStateChanged(boolean provisioned)80         public void onSatelliteProvisionStateChanged(boolean provisioned) {
81             mProvisioned = provisioned;
82             Log.d(TAG, "onSatelliteProvisionStateChanged in SatelliteTestApp: provisioned="
83                     + mProvisioned);
84         }
85     }
86 
provisionServiceApp(View view)87     private void provisionServiceApp(View view) {
88         TextView textView = findViewById(R.id.text_id);
89         CancellationSignal cancellationSignal = new CancellationSignal();
90         LinkedBlockingQueue<Integer> error = new LinkedBlockingQueue<>(1);
91         String mText = "This is test provision data.";
92         byte[] testProvisionData = mText.getBytes();
93         mSatelliteManager.provisionService("SATELLITE_TOKEN", testProvisionData,
94                 cancellationSignal, Runnable::run, error::offer);
95         try {
96             Integer value = error.poll(TIMEOUT, TimeUnit.MILLISECONDS);
97             if (value == null) {
98                 textView.setText("Timed out to provision the satellite");
99             } else if (value != SatelliteResult.SATELLITE_RESULT_SUCCESS) {
100                 textView.setText("Failed to provision SatelliteService with error = "
101                         + SatelliteErrorUtils.mapError(value));
102             } else {
103                 textView.setText("Successfully provisioned the satellite");
104             }
105         } catch (InterruptedException e) {
106             textView.setText("Provision SatelliteService exception caught =" + e);
107         }
108     }
109 
deprovisionServiceApp(View view)110     private void deprovisionServiceApp(View view) {
111         TextView textView = findViewById(R.id.text_id);
112         LinkedBlockingQueue<Integer> error = new LinkedBlockingQueue<>(1);
113         mSatelliteManager.deprovisionService("SATELLITE_TOKEN", Runnable::run,
114                 error::offer);
115         try {
116             Integer value = error.poll(TIMEOUT, TimeUnit.MILLISECONDS);
117             if (value == null) {
118                 textView.setText("Timed out to deprovision the satellite");
119             } else if (value != SatelliteResult.SATELLITE_RESULT_SUCCESS) {
120                 textView.setText("Failed to deprovision SatelliteService with error = "
121                         + SatelliteErrorUtils.mapError(value));
122             } else {
123                 textView.setText("Successfully deprovisioned the satellite");
124             }
125         } catch (InterruptedException e) {
126             textView.setText("Deprovision SatelliteService exception caught =" + e);
127         }
128     }
129 
requestIsProvisionedApp(View view)130     private void requestIsProvisionedApp(View view) {
131         final AtomicReference<Boolean> enabled = new AtomicReference<>();
132         final AtomicReference<Integer> errorCode = new AtomicReference<>();
133         OutcomeReceiver<Boolean, SatelliteManager.SatelliteException> mReceiver =
134                 new OutcomeReceiver<>() {
135             @Override
136             public void onResult(Boolean result) {
137                 enabled.set(result);
138                 TextView textView = findViewById(R.id.text_id);
139                 textView.setText("Status for requestIsSatelliteProvisioned result: "
140                         + enabled.get());
141             }
142 
143             @Override
144             public void onError(SatelliteManager.SatelliteException exception) {
145                 errorCode.set(exception.getErrorCode());
146                 TextView textView = findViewById(R.id.text_id);
147                 textView.setText("Status for requestIsSatelliteProvisioned error : "
148                         + SatelliteErrorUtils.mapError(errorCode.get()));
149             }
150         };
151         mSatelliteManager.requestIsProvisioned(Runnable::run, mReceiver);
152     }
153 
registerForProvisionStateChangedApp(View view)154     private void registerForProvisionStateChangedApp(View view) {
155         int result = mSatelliteManager.registerForProvisionStateChanged(Runnable::run,
156                 mCallback);
157         TextView textView = findViewById(R.id.text_id);
158         textView.setText("Status for registerForSatelliteProvisionStateChanged : "
159                 + SatelliteErrorUtils.mapError(result));
160     }
161 
unregisterForProvisionStateChangedApp(View view)162     private void unregisterForProvisionStateChangedApp(View view) {
163         mSatelliteManager.unregisterForProvisionStateChanged(mCallback);
164         TextView textView = findViewById(R.id.text_id);
165         textView.setText("unregisterForSatelliteProvisionStateChanged is successful");
166     }
167 
showCurrentSatelliteProvisionStateApp(View view)168     private void showCurrentSatelliteProvisionStateApp(View view) {
169         TextView textView = findViewById(R.id.text_id);
170         textView.setText("Current Provision State is " + mProvisioned);
171     }
172 
173     // Fetch the stored data in onResume()
174     // Because this is what will be called when the app opens again
175     @Override
onResume()176     protected void onResume() {
177         super.onResume();
178         // Fetching the stored data from the SharedPreference
179         SharedPreferences sh = getSharedPreferences("MySharedPref", MODE_PRIVATE);
180         boolean isProvisioned = sh.getBoolean("provision_state", mProvisioned);
181 
182         // Setting the fetched data
183         mProvisioned = isProvisioned;
184     }
185 
186     // Store the data in the SharedPreference in the onPause() method
187     // When the user closes the application onPause() will be called and data will be stored
188     @Override
onPause()189     protected void onPause() {
190         super.onPause();
191         // Creating a shared pref object with a file name "MySharedPref" in private mode
192         SharedPreferences sharedPreferences = getSharedPreferences("MySharedPref", MODE_PRIVATE);
193         SharedPreferences.Editor myEdit = sharedPreferences.edit();
194 
195         // write all the data entered by the user in SharedPreference and apply
196         myEdit.putBoolean("provision_state", mProvisioned);
197         myEdit.apply();
198     }
199 
onDestroy()200     protected void onDestroy() {
201         super.onDestroy();
202         SharedPreferences sharedPreferences = getSharedPreferences("MySharedPref", MODE_PRIVATE);
203 
204         final SharedPreferences.Editor sharedPrefsEditor = sharedPreferences.edit();
205         sharedPrefsEditor.remove("provision_state");
206         sharedPrefsEditor.commit();
207     }
208 }
209