Skip to content

Commit 93729d7

Browse files
author
xausky
committed
多处崩溃BUG修复
1 parent 698dab4 commit 93729d7

File tree

6 files changed

+88
-9
lines changed

6 files changed

+88
-9
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ android {
2020
applicationId "io.github.xausky.unitymodmanager"
2121
minSdkVersion 21
2222
targetSdkVersion 23
23-
versionCode 281
24-
versionName "2.8.1"
23+
versionCode 282
24+
versionName "2.8.2"
2525
ndk{
2626
abiFilters "armeabi-v7a","x86"
2727
}

app/src/main/java/io/github/xausky/unitymodmanager/ShortcutActivity.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.app.Activity;
44
import android.content.Context;
55
import android.content.Intent;
6+
import android.content.SharedPreferences;
67
import android.os.Bundle;
78
import android.support.annotation.Nullable;
89
import android.widget.Toast;
@@ -14,6 +15,8 @@
1415
import io.github.xausky.unitymodmanager.fragment.HomeFragment;
1516
import io.github.xausky.unitymodmanager.fragment.SettingFragment;
1617

18+
import static io.github.xausky.unitymodmanager.fragment.HomeFragment.APK_MODIFY_MODEL_VIRTUAL;
19+
1720
/**
1821
* Created by xausky on 4/7/18.
1922
*/
@@ -29,8 +32,25 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
2932
@Override
3033
protected void onPostCreate(@Nullable Bundle savedInstanceState) {
3134
super.onPostCreate(savedInstanceState);
32-
String launchPackage = getSharedPreferences(SettingFragment.SETTINGS_PREFERENCE_NAME, Context.MODE_PRIVATE).getString(HomeFragment.PACKAGE_PREFERENCE_KEY, null);
35+
SharedPreferences preferences = getSharedPreferences(SettingFragment.SETTINGS_PREFERENCE_NAME, Context.MODE_PRIVATE);
36+
int apkModifyModel = Integer.valueOf(preferences.getString("apk_modify_model", "1"));
37+
if(apkModifyModel != APK_MODIFY_MODEL_VIRTUAL){
38+
Toast.makeText(this, R.string.no_virtual_model_shortcut, Toast.LENGTH_LONG).show();
39+
finish();
40+
return;
41+
}
42+
String launchPackage = preferences.getString(HomeFragment.PACKAGE_PREFERENCE_KEY, null);
43+
if(launchPackage == null){
44+
Toast.makeText(this, R.string.install_source_not_found, Toast.LENGTH_LONG).show();
45+
finish();
46+
return;
47+
}
3348
Intent intent = VirtualCore.get().getLaunchIntent(launchPackage, 0);
49+
if(intent == null){
50+
Toast.makeText(this, R.string.install_source_not_found, Toast.LENGTH_LONG).show();
51+
finish();
52+
return;
53+
}
3454
VActivityManager.get().startActivity(intent, 0);
3555
finish();
3656
}

app/src/main/java/io/github/xausky/unitymodmanager/adapter/AttachesAdapter.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ public void setRecyclerView(RecyclerView view){
5858
public void update(String excludePackageName){
5959
this.excludePackageName = excludePackageName;
6060
applications.clear();
61-
List<InstalledAppInfo> installedApplications = VirtualCore.get().isStartup() ? VirtualCore.get().getInstalledApps(0): Collections.<InstalledAppInfo>emptyList();
61+
List<InstalledAppInfo> installedApplications = Collections.emptyList();
62+
try {
63+
installedApplications = VirtualCore.get().getInstalledApps(0);
64+
} catch (Exception e){
65+
e.printStackTrace();
66+
}
6267
for(InstalledAppInfo info: installedApplications){
6368
if(!info.packageName.equals(excludePackageName)) {
6469
applications.add(info.getApplicationInfo(0));

app/src/main/java/io/github/xausky/unitymodmanager/adapter/ModsAdapter.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import android.content.Context;
55
import android.content.DialogInterface;
66
import android.content.SharedPreferences;
7+
import android.graphics.Bitmap;
8+
import android.graphics.BitmapFactory;
79
import android.graphics.drawable.Drawable;
810
import android.support.v7.app.AlertDialog;
911
import android.support.v7.widget.DividerItemDecoration;
@@ -301,6 +303,34 @@ public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
301303
return new ViewHolder(view);
302304
}
303305

306+
// Decodes image and scales it to reduce memory consumption
307+
private Bitmap decodeFile(File f) {
308+
try {
309+
// Decode image size
310+
BitmapFactory.Options o = new BitmapFactory.Options();
311+
o.inJustDecodeBounds = true;
312+
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
313+
314+
// The new size we want to scale to
315+
final int REQUIRED_SIZE = 70;
316+
317+
// Find the correct scale value. It should be the power of 2.
318+
int scale = 1;
319+
while(o.outWidth / scale / 2 >= REQUIRED_SIZE &&
320+
o.outHeight / scale / 2 >= REQUIRED_SIZE) {
321+
scale *= 2;
322+
}
323+
324+
// Decode with inSampleSize
325+
BitmapFactory.Options o2 = new BitmapFactory.Options();
326+
o2.inSampleSize = scale;
327+
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
328+
} catch (FileNotFoundException e) {
329+
e.printStackTrace();
330+
}
331+
return null;
332+
}
333+
304334
@Override
305335
public void onBindViewHolder(ViewHolder holder, int position) {
306336
final Mod mod = mods.get(position);
@@ -346,7 +376,7 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
346376
if(file.isDirectory()){
347377
final File[] images = new File(mod.path + "/images").listFiles();
348378
if(images != null && images.length != 0){
349-
holder.icon.setImageDrawable(Drawable.createFromPath(images[0].getAbsolutePath()));
379+
holder.icon.setImageBitmap(decodeFile(images[0]));
350380
} else {
351381
holder.icon.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_mod_icon_default));
352382
}

app/src/main/java/io/github/xausky/unitymodmanager/fragment/HomeFragment.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.xausky.unitymodmanager.fragment;
22

3+
import android.app.Activity;
34
import android.app.PendingIntent;
45
import android.app.ProgressDialog;
56
import android.content.Context;
@@ -10,6 +11,7 @@
1011
import android.content.pm.PackageParser;
1112
import android.content.pm.ShortcutManager;
1213
import android.graphics.Bitmap;
14+
import android.graphics.Canvas;
1315
import android.graphics.drawable.BitmapDrawable;
1416
import android.graphics.drawable.Drawable;
1517
import android.net.sip.SipRegistrationListener;
@@ -475,7 +477,7 @@ public void run() {
475477
if (modFragment.getEnableItemCount() > 0) {
476478
modFragment.setNeedPatch(true);
477479
}
478-
if (modFragment.getItemCount() > 0) {
480+
if (modFragment.getItemCount() > 0 && !HomeFragment.this.getActivity().isFinishing()) {
479481
confirmDialog.show();
480482
}
481483
}
@@ -484,16 +486,38 @@ public void run() {
484486
}.start();
485487
}
486488

489+
public static Bitmap drawableToBitmap(Drawable drawable) {
490+
Bitmap bitmap = null;
491+
492+
if (drawable instanceof BitmapDrawable) {
493+
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
494+
if(bitmapDrawable.getBitmap() != null) {
495+
return bitmapDrawable.getBitmap();
496+
}
497+
}
498+
499+
if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
500+
bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
501+
} else {
502+
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
503+
}
504+
505+
Canvas canvas = new Canvas(bitmap);
506+
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
507+
drawable.draw(canvas);
508+
return bitmap;
509+
}
510+
487511
public void crateShortcut(InstalledAppInfo info) {
488512
if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)) {
489513
PackageManager manager = VirtualCore.get().getPackageManager();
490514
String name = manager.getApplicationLabel(info.getApplicationInfo(0)) + context.getString(R.string.shortcut_postfix);
491-
BitmapDrawable icon = (BitmapDrawable) manager.getApplicationIcon(info.getApplicationInfo(0));
515+
Drawable icon = manager.getApplicationIcon(info.getApplicationInfo(0));
492516
Intent shortcutInfoIntent = new Intent(Intent.ACTION_VIEW);
493517
shortcutInfoIntent.setClass(context, ShortcutActivity.class);
494518
shortcutInfoIntent.putExtra("io.github.xausky.unitymodmanager.launchPackage", info.packageName);
495519
ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(context, name)
496-
.setIcon(IconCompat.createWithBitmap(icon.getBitmap()))
520+
.setIcon(IconCompat.createWithBitmap(drawableToBitmap(icon)))
497521
.setShortLabel(name)
498522
.setIntent(shortcutInfoIntent)
499523
.build();

app/src/main/java/io/github/xausky/unitymodmanager/fragment/ModFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ public int patch(String apkPath, String baseApkPath, String persistentPath, Stri
286286
return ModUtils.RESULT_STATE_OBB_ERROR;
287287
}
288288
try {
289-
InputStream inputStream = context.getAssets().open("settings.xml");
289+
InputStream inputStream = getBase().getAssets().open("settings.xml");
290290
String settings = IOUtils.toString(inputStream);
291291
inputStream.close();
292292
settings = settings.replace("$CHECKSUM", ModUtils.checkSum(obbPath));

0 commit comments

Comments
 (0)