13
13
*/
14
14
package com .kalab .chess .enginesupport ;
15
15
16
- import java .io .File ;
17
- import java .io .FileNotFoundException ;
18
- import java .io .IOException ;
19
- import java .util .ArrayList ;
20
- import java .util .HashMap ;
21
- import java .util .List ;
22
- import java .util .Map ;
23
-
24
- import org .xmlpull .v1 .XmlPullParserException ;
25
-
26
16
import android .app .Activity ;
27
17
import android .content .Context ;
28
18
import android .content .Intent ;
29
19
import android .content .pm .ActivityInfo ;
30
- import android .content .pm .PackageInfo ;
20
+ import android .content .pm .ApplicationInfo ;
31
21
import android .content .pm .PackageManager ;
32
22
import android .content .pm .PackageManager .NameNotFoundException ;
33
23
import android .content .pm .ResolveInfo ;
37
27
import android .os .Bundle ;
38
28
import android .util .Log ;
39
29
30
+ import org .xmlpull .v1 .XmlPullParserException ;
31
+
32
+ import java .io .File ;
33
+ import java .io .IOException ;
34
+ import java .util .ArrayList ;
35
+ import java .util .HashMap ;
36
+ import java .util .List ;
37
+ import java .util .Map ;
38
+
40
39
public class ChessEngineResolver {
41
40
42
41
private static final String ENGINE_PROVIDER_MARKER = "intent.chess.provider.ENGINE" ;
@@ -46,12 +45,13 @@ public class ChessEngineResolver {
46
45
private Context context ;
47
46
private String target ;
48
47
/** map of package -> activity for license checks */
49
- Map <String , String > licenseCheckActivities = new HashMap <String , String >();
48
+ private Map <String , String > licenseCheckActivities = new HashMap <>();
50
49
50
+ @ SuppressWarnings ("deprecation" )
51
51
public ChessEngineResolver (Context context ) {
52
52
super ();
53
53
this .context = context ;
54
- this .target = Build .CPU_ABI ;
54
+ this .target = Build .CPU_ABI ; // use Build.SUPPORTED_ABIS[0] from API level 21 onwards
55
55
sanitizeArmV6Target ();
56
56
}
57
57
@@ -68,7 +68,7 @@ private void sanitizeArmV6Target() {
68
68
*/
69
69
public List <ChessEngine > resolveEngines () {
70
70
resolveLicenseCheckActivitiesPerPackage ();
71
- List <ChessEngine > result = new ArrayList <ChessEngine >();
71
+ List <ChessEngine > result = new ArrayList <>();
72
72
final Intent engineProviderIntent = new Intent (ENGINE_PROVIDER_MARKER );
73
73
List <ResolveInfo > engineProviderList = context .getPackageManager ()
74
74
.queryIntentActivities (engineProviderIntent ,
@@ -130,7 +130,7 @@ private List<ChessEngine> resolveEnginesForPackage(
130
130
}
131
131
132
132
private void parseEngineListXml (XmlResourceParser parser , String authority ,
133
- List <ChessEngine > result , String packageName ) {
133
+ List <ChessEngine > result , String packageName ) {
134
134
try {
135
135
int eventType = parser .getEventType ();
136
136
while (eventType != XmlResourceParser .END_DOCUMENT ) {
@@ -149,7 +149,7 @@ private void parseEngineListXml(XmlResourceParser parser, String authority,
149
149
}
150
150
151
151
private void addEngine (List <ChessEngine > result , XmlResourceParser parser ,
152
- String authority , String packageName ) {
152
+ String authority , String packageName ) {
153
153
if (parser .getName ().equalsIgnoreCase ("engine" )) {
154
154
String fileName = parser .getAttributeValue (null , "filename" );
155
155
String title = parser .getAttributeValue (null , "name" );
@@ -165,66 +165,18 @@ private void addEngine(List<ChessEngine> result, XmlResourceParser parser,
165
165
} catch (NameNotFoundException e ) {
166
166
Log .e (TAG , e .getMessage ());
167
167
}
168
- result .add (new ChessEngine (title , fileName , authority ,
169
- packageName , versionCode , licenseCheckActivities
170
- .get (packageName )));
171
- }
172
- }
173
- }
174
- }
175
-
176
- /**
177
- * Ensure that the engine is current. It re-copies the engine if it was not
178
- * current.
179
- *
180
- * @param fileName
181
- * the file name of the engine
182
- * @param packageName
183
- * the package name of the engine
184
- * @param versionCode
185
- * the (installed) version code of the engine
186
- * @param destination
187
- * the destination folder to copy a new engine to
188
- * @return the new version of the engine, -1 in case of an error (IOError or
189
- * if no engine was found)
190
- */
191
- public int ensureEngineVersion (String fileName , String packageName ,
192
- int versionCode , File destination ) {
193
- int result = -1 ;
194
- PackageInfo packageInfo ;
195
- Log .d (TAG , "checking engine " + fileName + ", " + packageName
196
- + ", version " + versionCode );
197
- try {
198
- packageInfo = context .getPackageManager ().getPackageInfo (
199
- packageName , 0 );
200
- if (packageInfo .versionCode > versionCode
201
- || !(new File (destination , fileName ).exists ())) {
202
- // package is updated or file is missing, need to copy engine
203
- // again
204
- for (ChessEngine engine : resolveEngines ()) {
205
- if (engine .getPackageName ().equals (packageName )
206
- && engine .getFileName ().equals (fileName )) {
207
- try {
208
- Log .d (TAG , "engine is outdated" );
209
- engine .copyToFiles (context .getContentResolver (),
210
- destination );
211
- result = packageInfo .versionCode ;
212
- } catch (FileNotFoundException e ) {
213
- Log .e (TAG , e .getMessage (), e );
214
- } catch (IOException e ) {
215
- Log .e (TAG , e .getMessage (), e );
216
- }
217
- break ;
168
+ try {
169
+ ApplicationInfo app = context .getPackageManager ().getApplicationInfo (packageName , 0 );
170
+ String enginePath = new File (app .nativeLibraryDir , fileName ).getAbsolutePath ();
171
+ result .add (new ChessEngine (title , fileName , enginePath , authority ,
172
+ packageName , versionCode , licenseCheckActivities
173
+ .get (packageName )));
174
+ } catch (NameNotFoundException e ) {
175
+ Log .e (TAG , e .getLocalizedMessage (), e );
218
176
}
219
177
}
220
- } else {
221
- Log .d (TAG , "engine is up-to-date" );
222
- result = packageInfo .versionCode ;
223
178
}
224
- } catch (NameNotFoundException e ) {
225
- Log .w (TAG , "package " + packageName + " not found" );
226
179
}
227
- return result ;
228
180
}
229
181
230
182
/**
@@ -242,7 +194,7 @@ public int ensureEngineVersion(String fileName, String packageName,
242
194
* If a license check is performed the caller must check the result in onActivityResult()
243
195
*/
244
196
public boolean checkLicense (Activity caller , int requestCode ,
245
- String fileName , String packageName ) {
197
+ String fileName , String packageName ) {
246
198
Log .d (TAG , "checking license for engine " + fileName + ", "
247
199
+ packageName );
248
200
for (ChessEngine engine : resolveEngines ()) {
0 commit comments