4package com.managexr.getserialsample;
7import android.content.ComponentName;
8import android.content.Context;
9import android.content.Intent;
10import android.content.ServiceConnection;
11import android.content.SharedPreferences;
12import android.content.pm.PackageInfo;
13import android.content.pm.PackageManager;
14import android.os.Bundle;
15import android.os.Handler;
16import android.os.IBinder;
17import android.os.Message;
18import android.os.Messenger;
19import android.os.RemoteException;
20import android.util.Log;
22import androidx.annotation.NonNull;
24import org.apache.commons.lang3.StringEscapeUtils;
25import org.json.JSONObject;
44 private final static String
TAG =
"MXRAdminAppMessenger";
110 if (serial.equals(currentSerial))
return;
113 SharedPreferences.Editor prefsEditor = prefs.edit();
114 prefsEditor.putString(
"serial", serial);
124 return context.getSharedPreferences(
context.getPackageName() +
".managexr_prefs", Context.MODE_PRIVATE);
135 Bundle bundle = msg.getData();
137 String deviceStatusJsonString = bundle.getString(
"json",
null);
138 if (deviceStatusJsonString ==
null)
return;
140 String serial = deviceStatusJson.getString(
"serial");
144 }
catch (Exception e) {
145 Log.v(
TAG,
"Failed to parse json: " + e.getMessage());
152 String noQuotes = uncleanJson.replaceAll(
"^\"|\"$",
"");
153 return StringEscapeUtils.unescapeJava(noQuotes);
172 Log.v(
TAG,
"tryBindToAdminService");
175 if (adminServiceComponent !=
null) {
177 Intent bindIntent =
new Intent();
178 bindIntent.setComponent(adminServiceComponent);
183 Log.v(
TAG,
"ManageXR Admin App not installed or QUERY_ALL_PACKAGES permissions was not added to the manifest!");
191 private final ServiceConnection
mConnection =
new ServiceConnection() {
192 public void onServiceConnected(ComponentName className, IBinder service) {
193 Log.v(
TAG,
"onServiceConnected");
197 if (registeredAsClient) {
198 Log.v(
TAG,
"Registered as client");
202 Log.e(
TAG,
"Failed to register as client. Unbinding...");
207 public void onServiceDisconnected(ComponentName className) {
208 Log.v(
TAG,
"onServiceDisconnected");
233 Message msg = Message.obtain(
null, what);
236 if (jsonString !=
null) {
237 Bundle bundle =
new Bundle();
238 bundle.putString(
"json", jsonString);
244 }
catch (RemoteException e) {
245 Log.e(
TAG, e.getMessage());
252 PackageManager pm =
context.getPackageManager();
253 List<PackageInfo> packages = pm.getInstalledPackages(0);
255 for (PackageInfo packageInfo : packages) {
256 if (packageInfo.packageName.startsWith(
"com.mightyimmersion.mightyplatform.adminapp") && !packageInfo.packageName.contains(
"preload")) {
264 Intent intent =
new Intent();
266 context.startForegroundService(intent);
static final int REGISTER_CLIENT
static final int DEVICE_STATUS
static final int GET_DEVICE_STATUS
void handleMessage(Message msg)
MXRAdminAppMessenger(Context _context)
final ServiceConnection mConnection
Messenger outgoingMessenger
boolean getDeviceStatusAsync()
void unbindFromAdminService()
SerialListener serialListener
void onSerialUpdated(SerialListener _listener)
boolean registerAsClient()
void startBindToAdminServiceLoop()
final int CHECK_BINDING_FREQUENCY
void tryBindToAdminService()
static final String ADMIN_SERVICE_CLASS_NAME
BindStatusListener bindStatusListener
boolean sendMessage(int what)
void bindToAdminServiceLoop()
void launchAdminAppServiceIfNeeded(String packageName)
final Messenger incomingMessenger
ComponentName getInstalledAdminAdminServiceComponent()
void onBindStatusUpdated(BindStatusListener _listener)
boolean sendMessage(int what, String jsonString)
String removeQuotesAndUnescape(String uncleanJson)
final Handler checkBindingHandler
SharedPreferences getSharedPrefs()
void updateSerial(@NonNull String serial)
void onBindStatusChanged(boolean bound)
void onSerialUpdated(String serial)