66import android .content .pm .Signature ;
77import android .database .Cursor ;
88import android .net .Uri ;
9+ import android .os .AsyncTask ;
910import android .util .Log ;
1011
1112import java .util .Base64 ;
@@ -21,173 +22,12 @@ public class DIHelper {
2122 // This method will return the serial number in the string passed through the onSuccess method
2223 public static void getSerialNumber (Context context , IDIResultCallbacks callbackInterface )
2324 {
24- registerCurrentApplication (context , Uri .parse ("content://oem_info/oem.zebra.secure/build_serial" ), new IDIResultCallbacks () {
25- @ Override
26- public void onSuccess (String message ) {
27- String test = message ;
28- }
29-
30- @ Override
31- public void onError (String message ) {
32- String test = message ;
33-
34- }
35-
36- @ Override
37- public void onDebugStatus (String message ) {
38- String test = message ;
39-
40- }
41- });
42- RetrieveOEMInfo (context , Uri .parse ("content://oem_info/oem.zebra.secure/build_serial" ), callbackInterface );
25+ new RetrieveOEMInfoTask ().execute (context , Uri .parse ("content://oem_info/oem.zebra.secure/build_serial" ), callbackInterface );
4326 }
4427
4528 // This method will return the imei number in the string passed through the onSuccess method
4629 public static void getIMEINumber (Context context , IDIResultCallbacks callbackInterface )
4730 {
48- RetrieveOEMInfo (context , Uri .parse ("content://oem_info/wan/imei" ), callbackInterface );
49- }
50-
51- private static void registerCurrentApplication (Context context , Uri serviceIdentifier , IDIResultCallbacks callbackInterface )
52- {
53- String profileName = "AccessMgr-1" ;
54- String profileData = "" ;
55- try {
56- PackageInfo packageInfo = context .getPackageManager ().getPackageInfo (context .getPackageName (), PackageManager .GET_SIGNING_CERTIFICATES );
57- String path = context .getApplicationInfo ().sourceDir ;
58- final String strName = packageInfo .applicationInfo .loadLabel (context .getPackageManager ()).toString ();
59- final String strVendor = packageInfo .packageName ;
60- Signature sig = apkCertificate ;
61-
62- // Let's check if we have a custom certificate
63- if (sig == null )
64- {
65- // Nope, we will get the first apk signing certificate that we find
66- // You can copy/paste this snippet if you want to provide your own
67- // certificate
68- // TODO: use the following code snippet to extract your custom certificate if necessary
69- final Signature [] arrSignatures = packageInfo .signingInfo .getApkContentsSigners ();
70- if (arrSignatures == null || arrSignatures .length == 0 )
71- {
72- if (callbackInterface != null )
73- {
74- callbackInterface .onError ("Error : Package has no signing certificates... how's that possible ?" );
75- return ;
76- }
77- }
78- sig = arrSignatures [0 ];
79- }
80-
81- /*
82- * Get the X.509 certificate.
83- */
84- final byte [] rawCert = sig .toByteArray ();
85-
86- // Get the certificate as a base64 string
87- String encoded = Base64 .getEncoder ().encodeToString (rawCert );
88-
89- profileData =
90- "<?xml version=\" 1.0\" encoding=\" utf-8\" ?>" +
91- "<characteristic type=\" Profile\" >" +
92- "<parm name=\" ProfileName\" value=\" " + profileName + "\" />" +
93- "<characteristic type=\" AccessMgr\" version=\" 9.2\" >" +
94- "<parm name=\" OperationMode\" value=\" 1\" />" +
95- "<parm name=\" ServiceAccessAction\" value=\" 4\" />" +
96- "<parm name=\" ServiceIdentifier\" value=\" " + serviceIdentifier + "\" />" +
97- "<parm name=\" CallerPackageName\" value=\" " + context .getPackageName ().toString () + "\" />" +
98- "<parm name=\" CallerSignature\" value=\" " + encoded + "\" />" +
99- "</characteristic>" +
100- "</characteristic>" ;
101- DIProfileManagerCommand profileManagerCommand = new DIProfileManagerCommand (context );
102- profileManagerCommand .execute (profileData , profileName , callbackInterface );
103- //}
104- } catch (Exception e ) {
105- e .printStackTrace ();
106- if (callbackInterface != null )
107- {
108- callbackInterface .onError ("Error on profile: " + profileName + "\n Error:" + e .getLocalizedMessage () + "\n ProfileData:" + profileData );
109- }
110- }
111- }
112-
113- private static void RetrieveOEMInfo (final Context context , final Uri uri , final IDIResultCallbacks callbackInterface ) {
114- // For clarity, this code calls ContentResolver.query() on the UI thread but production code should perform queries asynchronously.
115- // See https://developer.android.com/guide/topics/providers/content-provider-basics.html for more information
116- Cursor cursor = context .getContentResolver ().query (uri , null , null , null , null );
117- if (cursor == null || cursor .getCount () < 1 )
118- {
119- if (callbackInterface != null )
120- {
121- callbackInterface .onDebugStatus ("App not registered to call OEM Service:" + uri .toString ()+"\n Registering current application using profile manger, this may take a couple of seconds..." );
122- }
123- // Let's register the application
124- registerCurrentApplication (context , uri , new IDIResultCallbacks () {
125- @ Override
126- public void onSuccess (String message ) {
127- // The app has been registered
128- // Let's try again to get the identifier
129- Cursor cursor2 = context .getContentResolver ().query (uri , null , null , null , null );
130- if (cursor2 == null || cursor2 .getCount () < 1 ) {
131- if (callbackInterface != null )
132- {
133- callbackInterface .onError ("Fail to register the app for OEM Service call:" + uri + "\n It's time to debug this app ;)" );
134- return ;
135- }
136- }
137- getURIValue (cursor2 , uri , callbackInterface );
138- return ;
139- }
140-
141- @ Override
142- public void onError (String message ) {
143- if (callbackInterface != null )
144- {
145- callbackInterface .onError (message );
146- return ;
147- }
148- }
149-
150- @ Override
151- public void onDebugStatus (String message ) {
152- if (callbackInterface != null )
153- {
154- callbackInterface .onDebugStatus (message );
155- }
156- }
157- });
158- }
159- else
160- {
161- // We have the right to call this service, and we obtained some data to parse...
162- getURIValue (cursor , uri , callbackInterface );
163- }
164- }
165-
166- private static void getURIValue (Cursor cursor , Uri uri , IDIResultCallbacks resultCallbacks )
167- {
168- while (cursor .moveToNext ()) {
169- if (cursor .getColumnCount () == 0 )
170- {
171- // No data in the cursor. I have seen this happen on non-WAN devices
172- String errorMsg = "Error: " + uri + " does not exist on this device" ;
173- resultCallbacks .onDebugStatus (errorMsg );
174- }
175- else {
176- for (int i = 0 ; i < cursor .getColumnCount (); i ++) {
177- try {
178- String data = cursor .getString (cursor .getColumnIndex (cursor .getColumnName (i )));
179- resultCallbacks .onSuccess (data );
180- cursor .close ();
181- return ;
182- }
183- catch (Exception e )
184- {
185- resultCallbacks .onDebugStatus (e .getLocalizedMessage ());
186- }
187- }
188- }
189- }
190- cursor .close ();
191- resultCallbacks .onError ("Data not found in Uri:" + uri );
31+ new RetrieveOEMInfoTask ().execute (context , Uri .parse ("content://oem_info/wan/imei" ), callbackInterface );
19232 }
19333}
0 commit comments