Skip to content

Commit

Permalink
Oreo fixes - icon loading.
Browse files Browse the repository at this point in the history
  • Loading branch information
kenumir committed Oct 25, 2018
1 parent 5420152 commit 52bb23a
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 117 deletions.
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ apply from: '../dependencies.gradle'

android {
compileSdkVersion 28
//buildToolsVersion '28.0.2'

defaultConfig {
applicationId "com.wt.apkinfo"
minSdkVersion 15
targetSdkVersion 28
versionCode 8
versionName "1.1.5"
versionCode 9
versionName "1.1.6"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

resConfigs "en", "pl"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.AdaptiveIconDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
Expand Down Expand Up @@ -38,11 +40,15 @@
import com.google.firebase.perf.metrics.AddTrace;
import com.hivedi.console.Console;
import com.hivedi.era.ERA;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;
import com.wt.apkinfo.R;
import com.wt.apkinfo.dialog.InfoListDialog;
import com.wt.apkinfo.entity.ApplicationDetailsEntity;
import com.wt.apkinfo.entity.ComponentInfo;
import com.wt.apkinfo.util.BitmapUtil;
import com.wt.apkinfo.util.DateTime;
import com.wt.apkinfo.util.ImageLoader;
import com.wt.apkinfo.util.ViewUtil;
import com.wt.apkinfo.viewmodel.ApplicationDetailsViewModel;

Expand All @@ -59,7 +65,7 @@
*
*/

public class ApplicationDetailsActivity extends AppCompatActivity implements InfoListDialog.OnGetData {
public class ApplicationDetailsActivity extends AppCompatActivity implements InfoListDialog.OnGetData, Target {

public static final String KEY_APP_ID = "application_id";
public static final String KEY_APP_NAME = "application_name";
Expand All @@ -71,24 +77,24 @@ public static void start(@NonNull Activity ctx, @NonNull String appId, @NonNull

// Bug in activity transitions, fixed in android 6.x
// https://issuetracker.google.com/issues/37121916
//if (Build.VERSION.SDK_INT >= 23 && Build.VERSION.SDK_INT < 27) {
// View decorView = ctx.getWindow().getDecorView();
// View statusBar = decorView.findViewById(android.R.id.statusBarBackground);
// View navigationBar = decorView.findViewById(android.R.id.navigationBarBackground);
// List<Pair<View, String>> el = new ArrayList<>();
// el.add(Pair.create(holderImage, "transition_" + appId));
// if (statusBar != null) {
// el.add(Pair.create(statusBar, Window.STATUS_BAR_BACKGROUND_TRANSITION_NAME));
// }
// if (navigationBar != null) {
// el.add(Pair.create(navigationBar, Window.NAVIGATION_BAR_BACKGROUND_TRANSITION_NAME));
// }
// //noinspection unchecked
// ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(ctx, el.toArray(new Pair[0]));
// ctx.startActivity(it, options.toBundle());
//} else {
if (Build.VERSION.SDK_INT >= 23) {
View decorView = ctx.getWindow().getDecorView();
View statusBar = decorView.findViewById(android.R.id.statusBarBackground);
View navigationBar = decorView.findViewById(android.R.id.navigationBarBackground);
List<Pair<View, String>> el = new ArrayList<>();
el.add(Pair.create(holderImage, "transition_" + appId));
if (statusBar != null) {
el.add(Pair.create(statusBar, Window.STATUS_BAR_BACKGROUND_TRANSITION_NAME));
}
if (navigationBar != null) {
el.add(Pair.create(navigationBar, Window.NAVIGATION_BAR_BACKGROUND_TRANSITION_NAME));
}
//noinspection unchecked
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(ctx, el.toArray(new Pair[0]));
ctx.startActivity(it, options.toBundle());
} else {
ctx.startActivity(it);
//}
}
}

@BindView(R.id.toolbar) Toolbar toolbar;
Expand Down Expand Up @@ -126,10 +132,9 @@ public void onChanged(final @Nullable ApplicationDetailsEntity productEntity) {
if (productEntity != null) {
toolbar.setTitle(productEntity.getName());
toolbar.setSubtitle(productEntity.getId());
//toolbar.setNavigationIcon(productEntity.getIcon36dp(ApplicationDetailsActivity.this));
toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
//Console.loge("R=" + productEntity.icon.mutate());
//AdaptiveIconDrawable a;

ImageLoader.get().load(productEntity.getIconUri(), ApplicationDetailsActivity.this);

mAppInfoAdapter.setData(productEntity);
supportStartPostponedEnterTransition();
Expand Down Expand Up @@ -220,18 +225,18 @@ public void onClick(View view) {
}
});
toolbar.setNavigationContentDescription(appId);
//if (Build.VERSION.SDK_INT >= 23) {
// supportPostponeEnterTransition();
// View navIcon = ViewUtil.findViewWithContentDescription(toolbar, appId);
// if (navIcon != null) {
// navIcon.setTransitionName("transition_" + appId);
// }
// Transition fade = new Fade();
// fade.excludeTarget(android.R.id.statusBarBackground, true);
// fade.excludeTarget(android.R.id.navigationBarBackground, true);
// getWindow().setExitTransition(fade);
// getWindow().setEnterTransition(fade);
//}
if (Build.VERSION.SDK_INT >= 23) {
supportPostponeEnterTransition();
View navIcon = ViewUtil.findViewWithContentDescription(toolbar, appId);
if (navIcon != null) {
navIcon.setTransitionName("transition_" + appId);
}
Transition fade = new Fade();
fade.excludeTarget(android.R.id.statusBarBackground, true);
fade.excludeTarget(android.R.id.navigationBarBackground, true);
getWindow().setExitTransition(fade);
getWindow().setEnterTransition(fade);
}

mAppInfoAdapter = new AppInfoAdapter(getResources(), new OnHeaderClick() {
@Override
Expand Down Expand Up @@ -280,6 +285,21 @@ public List<ComponentInfo> onGetData() {
return null;
}

@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
toolbar.setNavigationIcon(BitmapUtil.bitmapToDrawable(this, bitmap));
}

@Override
public void onBitmapFailed(Drawable errorDrawable) {
toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
}

@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
}

private static class ItemHeader {
public String title;
public ComponentInfo[] data;
Expand Down
6 changes: 0 additions & 6 deletions app/src/main/java/com/wt/apkinfo/db/AppInfoTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,6 @@ protected ApplicationDetailsEntity doInBackground(Context... contexts) {
*/
result.id = appId;
result.name = pi.applicationInfo.loadLabel(pm).toString();
//result.icon = pi.applicationInfo.loadIcon(pm);

//Bitmap src = BitmapUtil.drawableToBitmap(result.icon);
//int dp36 = (int) (context.getResources().getDisplayMetrics().density * 36f);
//result.icon36dp = result.icon;
//result.icon36dp = BitmapUtil.bitmapToDrawable(context, Bitmap.createScaledBitmap(src, dp36, dp36, true));

int counter;

Expand Down
13 changes: 1 addition & 12 deletions app/src/main/java/com/wt/apkinfo/db/AppListTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,7 @@ protected List<ApplicationEntity> doInBackground(Context... params) {
}

e.iconUri = "app://" + ri.activityInfo.packageName;
//Console.logi("SRC=" + ri.activityInfo.applicationInfo.publicSourceDir);
//if (ri.activityInfo.icon == 0) {
// e.iconUri = ri.activityInfo.packageName + "/" + ri.activityInfo.applicationInfo.;
//} else {
// e.iconUri = ri.activityInfo.packageName + "/" + ri.activityInfo.targetActivity;
//}

//if (ri.activityInfo.icon == 0) {
//e.icon = ri.activityInfo.applicationInfo.loadIcon(pm);
//} else {
// e.icon = ri.activityInfo.loadIcon(pm);
//}

list.add(e);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ public class ApplicationDetailsEntity implements ApplicationDetailsModel {

public String id;
public String name;
//public Drawable icon;
//public Drawable icon36dp;
public ComponentInfo[] activities;
public ComponentInfo[] services;
public ComponentInfo[] permissions;
Expand All @@ -46,28 +44,9 @@ public String getName() {
}

@Override
public Drawable getIcon() {
return null; // icon;
public String getIconUri() {
return "app://" + id;
}

@Override
public Drawable getIcon36dp(Context ctx) {
//if (icon != null && icon36dp == null) {
// int dp36 = (int) (ctx.getResources().getDisplayMetrics().density * 36f);
// Bitmap src = BitmapUtil.drawableToBitmap(icon);
// icon36dp = BitmapUtil.bitmapToDrawable(ctx, Bitmap.createScaledBitmap(src, dp36, dp36, true));
//}
//if (icon36dp == null) {
// PackageManager pm = ctx.getPackageManager();
// try {
// PackageInfo pi = pm.getPackageInfo(id, PackageManager.GET_ACTIVITIES);
// icon36dp = pi.applicationInfo.loadIcon(pm);
// } catch (Exception e) {
// // ignore
// }
//}
//return icon36dp;
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public class ApplicationEntity implements ApplicationModel, Comparable<Applicati

public String id;
public String name;
public Drawable icon;
public String iconUri;

@Override
Expand All @@ -27,11 +26,6 @@ public String getName() {
return name;
}

@Override
public Drawable getIcon() {
return icon;
}

@Override
public String getIconUri() {
return iconUri;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,11 @@ private static class ApplicationsListAdapter extends RecyclerView.Adapter<Applic

private List<ApplicationEntity> mData;
private OnItemClick mOnItemClick;
private ImageLoader mImageLoader;

ApplicationsListAdapter(OnItemClick click) {
mOnItemClick = click;
mImageLoader = ImageLoader.get();
}

public void setData(List<ApplicationEntity> d) {
Expand All @@ -226,7 +228,6 @@ public void onBindViewHolder(@NonNull final ApplicationsItemHolder holder, int p
final ApplicationEntity entry = mData.get(position);
holder.text1.setText(entry.getName());
holder.text2.setText(entry.getId());
//holder.icon1.setImageDrawable(entry.getIcon());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Expand All @@ -236,7 +237,7 @@ public void onClick(View view) {
}
});

ImageLoader.get().load(entry.getIconUri(), holder.icon1);
mImageLoader.load(entry.getIconUri(), holder.icon1);

holder.update(entry);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public interface ApplicationDetailsModel {

String getId();
String getName();
Drawable getIcon();
Drawable getIcon36dp(Context ctx);
String getIconUri();

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public interface ApplicationModel {

String getId();
String getName();
Drawable getIcon();
String getIconUri();

}
40 changes: 15 additions & 25 deletions app/src/main/java/com/wt/apkinfo/util/ImageLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Request;
import com.squareup.picasso.RequestHandler;
import com.squareup.picasso.Target;
import com.wt.apkinfo.BuildConfig;
import com.wt.apkinfo.R;

import java.io.IOException;

Expand Down Expand Up @@ -49,18 +51,19 @@ private ImageLoader(Context ctx) {
.addRequestHandler(new RequestHandler() {
@Override
public boolean canHandleRequest(Request data) {
return data.uri.getScheme().equals("app");
String scheme = data.uri.getScheme();
return "app".equals(scheme);
}
@Override
public Result load(Request request, int networkPolicy) throws IOException {
try {
return new Result(
drawableToBitmap(pm.getApplicationIcon(request.uri.getHost())),
BitmapUtil.drawableToBitmap(pm.getApplicationIcon(request.uri.getHost())),
Picasso.LoadedFrom.DISK
);
} catch (Exception e) {
if (BuildConfig.DEBUG) {
Console.loge("" + e, e);
Console.loge("ImageLoader.load: " + e, e);
}
}
return null;
Expand All @@ -69,32 +72,19 @@ public Result load(Request request, int networkPolicy) throws IOException {
.build();
}

private 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 load(String url, ImageView img) {
mPicasso.cancelRequest(img);
mPicasso.load(url)
.fit()
.centerInside()
.into(img);
}

public void load(String url, Target img) {
mPicasso.load(url)
//.fit()
.resizeDimen(R.dimen.app_icon_size, R.dimen.app_icon_size)
.centerInside()
.into(img);
}
}
7 changes: 4 additions & 3 deletions app/src/main/res/layout/item_application.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
android:layout_height="wrap_content"
android:padding="16dp"
android:background="?attr/selectableItemBackground"
android:clickable="true">
android:clickable="true"
android:focusable="true">

<ImageView
android:id="@+id/icon1"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_width="@dimen/app_icon_size"
android:layout_height="@dimen/app_icon_size"
android:scaleType="fitCenter"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<resources>

<dimen name="app_icon_size">40dp</dimen>
</resources>

0 comments on commit 52bb23a

Please sign in to comment.