Skip to content

Commit

Permalink
多处崩溃BUG修复
Browse files Browse the repository at this point in the history
  • Loading branch information
xausky committed Dec 26, 2018
1 parent 698dab4 commit 93729d7
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 9 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ android {
applicationId "io.github.xausky.unitymodmanager"
minSdkVersion 21
targetSdkVersion 23
versionCode 281
versionName "2.8.1"
versionCode 282
versionName "2.8.2"
ndk{
abiFilters "armeabi-v7a","x86"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.Toast;
Expand All @@ -14,6 +15,8 @@
import io.github.xausky.unitymodmanager.fragment.HomeFragment;
import io.github.xausky.unitymodmanager.fragment.SettingFragment;

import static io.github.xausky.unitymodmanager.fragment.HomeFragment.APK_MODIFY_MODEL_VIRTUAL;

/**
* Created by xausky on 4/7/18.
*/
Expand All @@ -29,8 +32,25 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
@Override
protected void onPostCreate(@Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
String launchPackage = getSharedPreferences(SettingFragment.SETTINGS_PREFERENCE_NAME, Context.MODE_PRIVATE).getString(HomeFragment.PACKAGE_PREFERENCE_KEY, null);
SharedPreferences preferences = getSharedPreferences(SettingFragment.SETTINGS_PREFERENCE_NAME, Context.MODE_PRIVATE);
int apkModifyModel = Integer.valueOf(preferences.getString("apk_modify_model", "1"));
if(apkModifyModel != APK_MODIFY_MODEL_VIRTUAL){
Toast.makeText(this, R.string.no_virtual_model_shortcut, Toast.LENGTH_LONG).show();
finish();
return;
}
String launchPackage = preferences.getString(HomeFragment.PACKAGE_PREFERENCE_KEY, null);
if(launchPackage == null){
Toast.makeText(this, R.string.install_source_not_found, Toast.LENGTH_LONG).show();
finish();
return;
}
Intent intent = VirtualCore.get().getLaunchIntent(launchPackage, 0);
if(intent == null){
Toast.makeText(this, R.string.install_source_not_found, Toast.LENGTH_LONG).show();
finish();
return;
}
VActivityManager.get().startActivity(intent, 0);
finish();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ public void setRecyclerView(RecyclerView view){
public void update(String excludePackageName){
this.excludePackageName = excludePackageName;
applications.clear();
List<InstalledAppInfo> installedApplications = VirtualCore.get().isStartup() ? VirtualCore.get().getInstalledApps(0): Collections.<InstalledAppInfo>emptyList();
List<InstalledAppInfo> installedApplications = Collections.emptyList();
try {
installedApplications = VirtualCore.get().getInstalledApps(0);
} catch (Exception e){
e.printStackTrace();
}
for(InstalledAppInfo info: installedApplications){
if(!info.packageName.equals(excludePackageName)) {
applications.add(info.getApplicationInfo(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.DividerItemDecoration;
Expand Down Expand Up @@ -301,6 +303,34 @@ public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(view);
}

// Decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f) {
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);

// The new size we want to scale to
final int REQUIRED_SIZE = 70;

// Find the correct scale value. It should be the power of 2.
int scale = 1;
while(o.outWidth / scale / 2 >= REQUIRED_SIZE &&
o.outHeight / scale / 2 >= REQUIRED_SIZE) {
scale *= 2;
}

// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
final Mod mod = mods.get(position);
Expand Down Expand Up @@ -346,7 +376,7 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(file.isDirectory()){
final File[] images = new File(mod.path + "/images").listFiles();
if(images != null && images.length != 0){
holder.icon.setImageDrawable(Drawable.createFromPath(images[0].getAbsolutePath()));
holder.icon.setImageBitmap(decodeFile(images[0]));
} else {
holder.icon.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_mod_icon_default));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.xausky.unitymodmanager.fragment;

import android.app.Activity;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.Context;
Expand All @@ -10,6 +11,7 @@
import android.content.pm.PackageParser;
import android.content.pm.ShortcutManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.sip.SipRegistrationListener;
Expand Down Expand Up @@ -475,7 +477,7 @@ public void run() {
if (modFragment.getEnableItemCount() > 0) {
modFragment.setNeedPatch(true);
}
if (modFragment.getItemCount() > 0) {
if (modFragment.getItemCount() > 0 && !HomeFragment.this.getActivity().isFinishing()) {
confirmDialog.show();
}
}
Expand All @@ -484,16 +486,38 @@ public void run() {
}.start();
}

public static Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap = null;

if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
if(bitmapDrawable.getBitmap() != null) {
return bitmapDrawable.getBitmap();
}
}

if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
} else {
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
}

Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}

public void crateShortcut(InstalledAppInfo info) {
if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)) {
PackageManager manager = VirtualCore.get().getPackageManager();
String name = manager.getApplicationLabel(info.getApplicationInfo(0)) + context.getString(R.string.shortcut_postfix);
BitmapDrawable icon = (BitmapDrawable) manager.getApplicationIcon(info.getApplicationInfo(0));
Drawable icon = manager.getApplicationIcon(info.getApplicationInfo(0));
Intent shortcutInfoIntent = new Intent(Intent.ACTION_VIEW);
shortcutInfoIntent.setClass(context, ShortcutActivity.class);
shortcutInfoIntent.putExtra("io.github.xausky.unitymodmanager.launchPackage", info.packageName);
ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(context, name)
.setIcon(IconCompat.createWithBitmap(icon.getBitmap()))
.setIcon(IconCompat.createWithBitmap(drawableToBitmap(icon)))
.setShortLabel(name)
.setIntent(shortcutInfoIntent)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ public int patch(String apkPath, String baseApkPath, String persistentPath, Stri
return ModUtils.RESULT_STATE_OBB_ERROR;
}
try {
InputStream inputStream = context.getAssets().open("settings.xml");
InputStream inputStream = getBase().getAssets().open("settings.xml");
String settings = IOUtils.toString(inputStream);
inputStream.close();
settings = settings.replace("$CHECKSUM", ModUtils.checkSum(obbPath));
Expand Down

0 comments on commit 93729d7

Please sign in to comment.