Skip to content

Commit

Permalink
Merge pull request #164 from Trumeet/dev
Browse files Browse the repository at this point in the history
DEV-20180727
  • Loading branch information
TimothyZhang023 committed Jul 27, 2018
2 parents 9688257 + dc48eb2 commit 909777f
Show file tree
Hide file tree
Showing 20 changed files with 96 additions and 163 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Expand Up @@ -15,9 +15,9 @@ cache:
- $HOME/.gradle/caches/
- $HOME/.gradle/wrapper/
script:
- "./gradlew exportVersion"
- "./gradlew :app:assembleRelease"
- "./gradlew :push:assembleRelease"
- "./gradlew exportVersion --daemon"
- "./gradlew :app:assembleRelease --daemon --parallel"
- "./gradlew :push:assembleRelease --daemon --parallel"
before_install:
- yes | sdkmanager "platforms;android-27"
- openssl aes-256-cbc -K $encrypted_66a7d5f7594e_key -iv $encrypted_66a7d5f7594e_iv
Expand Down
14 changes: 2 additions & 12 deletions app/src/main/java/top/trumeet/mipushframework/MainActivity.java
Expand Up @@ -248,18 +248,8 @@ private synchronized void showConnectFail(@OnConnectStatusChangedListener.FailRe
(mController != null && mController.isConnected()) ?
mController.getVersionCode() : -1))
.setCancelable(false)
.setPositiveButton(R.string.retry, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
checkAndConnect();
}
})
.setNegativeButton(R.string.exit, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setPositiveButton(R.string.retry, (dialog, which) -> checkAndConnect())
.setNegativeButton(R.string.exit, (dialog, which) -> finish())
.show();
if (mController != null && mController.isConnected()) {
mController.disconnectIfNeeded();
Expand Down
Expand Up @@ -48,12 +48,7 @@ public void onTerminate() {

private void init() {
RxActivityResult.register(this);
//debugIcon init
try {
SharedPreferences prefs = PreferencesUtils.getPreferences(this);
BaseAppsBinder.debugIcon = prefs.getBoolean(PreferencesUtils.KEY_DEBUG_ICON, false);
} catch (RemotePreferenceAccessException e) {
}

}

}
Expand Up @@ -146,7 +146,7 @@ protected List<RegisteredApplication> doInBackground(Integer... integers) {
@Override
protected void onPostExecute(List<RegisteredApplication> list) {

boolean showWarn = (list.size() == 0);
boolean showWarn = false;
for (RegisteredApplication registeredApplication : list) {
if (!registeredApplication.isRegistered()) {
showWarn = true;
Expand Down
Expand Up @@ -20,7 +20,9 @@
import android.widget.TextView;

import me.drakeet.multitype.ItemViewBinder;
import top.trumeet.common.cache.ApplicationNameCache;
import top.trumeet.common.cache.IconCache;
import top.trumeet.mipush.BuildConfig;
import top.trumeet.mipush.R;

/**
Expand All @@ -32,16 +34,10 @@

public abstract class BaseAppsBinder<T> extends ItemViewBinder<T, BaseAppsBinder.ViewHolder> {

private LruCache<String, Drawable> mIconMemoryCaches;

public static boolean debugIcon = false;
public static boolean debugIcon = BuildConfig.DEBUG;

public BaseAppsBinder() {
super();
int maxMemory = (int) Runtime.getRuntime().maxMemory();
int cacheSizes = maxMemory / 5;

mIconMemoryCaches = new LruCache<>(cacheSizes);
}

@NonNull
Expand All @@ -52,22 +48,20 @@ protected final ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater,
}

public final void fillData(String pkgName, boolean fillName, ViewHolder holder) {
Context context = holder.itemView.getContext();
if (fillName) {
PackageManager packageManager = holder.itemView.getContext().
getPackageManager();
try {
holder.title.setText(packageManager.getApplicationLabel(packageManager.getApplicationInfo(pkgName,
0)));
} catch (PackageManager.NameNotFoundException e) {
holder.title.setText(pkgName);
CharSequence appName = ApplicationNameCache.getInstance().getAppName(context, pkgName);
if (appName == null) {
appName = pkgName;
}
holder.title.setText(appName);
}
Drawable icon = getIconFromMemoryCache(pkgName);

Bitmap icon = IconCache.getInstance().getRawIconBitmapWithoutLoader(context, pkgName);
if (icon != null) {
holder.icon.setImageDrawable(icon);
holder.icon.setImageBitmap(icon);
} else {
new IconWorkerTask(holder.icon)
.execute(pkgName);
new IconWorkerTask(holder.icon).execute(pkgName);
}
}

Expand All @@ -84,19 +78,7 @@ public final void fillData(String pkgName, ViewHolder holder) {
fillData(pkgName, true, holder);
}

private void addDrawableToMemoryCache(@Nullable String pkg, @NonNull Drawable icon) {
if (getIconFromMemoryCache(pkg) == null) {
if (pkg == null) pkg = "";
mIconMemoryCaches.put(pkg, icon);
}
}

private Drawable getIconFromMemoryCache(@Nullable String pkg) {
if (pkg == null) pkg = "";
return mIconMemoryCaches.get(pkg);
}

private class IconWorkerTask extends AsyncTask<String, Void, Drawable> {
private class IconWorkerTask extends AsyncTask<String, Void, Bitmap> {
private Context context;

private ImageView imageView;
Expand All @@ -107,42 +89,27 @@ private class IconWorkerTask extends AsyncTask<String, Void, Drawable> {
}

@Override
protected Drawable doInBackground(String... params) {
protected Bitmap doInBackground(String... params) {
String pkg = params[0];
Drawable icon;
if (TextUtils.isEmpty(pkg)) {
pkg = "";
icon = ContextCompat.getDrawable(context, android.R.mipmap.sym_def_app_icon);
} else {
Bitmap icon = null;
if (!TextUtils.isEmpty(pkg)) {
if (debugIcon) {
Bitmap whiteIconBitmap = IconCache.getInstance().getWhiteIconBitmap(context, pkg);
if (whiteIconBitmap != null) {
icon = new BitmapDrawable(context.getResources(), whiteIconBitmap);
} else {
icon = ContextCompat.getDrawable(context, android.R.mipmap.sym_def_app_icon);
}
icon = IconCache.getInstance().getWhiteIconBitmap(context, pkg);
} else {
try {
icon = context.getPackageManager()
.getApplicationIcon(pkg);
} catch (PackageManager.NameNotFoundException ignore) {
icon = ContextCompat.getDrawable(context,
android.R.mipmap.sym_def_app_icon);
}
icon = IconCache.getInstance().getRawIconBitmap(context, pkg);
}

}
if (icon == null) {
return null;
Drawable drawable = ContextCompat.getDrawable(context, android.R.mipmap.sym_def_app_icon);

}
addDrawableToMemoryCache(pkg, icon);
return icon;
}

@Override
protected void onPostExecute(Drawable drawable) {
protected void onPostExecute(Bitmap bitmap) {
if (imageView != null) {
imageView.setImageDrawable(drawable);
imageView.setImageBitmap(bitmap);
if (debugIcon) {
imageView.setBackgroundColor(Color.BLACK);
}
Expand Down
5 changes: 0 additions & 5 deletions app/src/main/res/xml/settings.xml
Expand Up @@ -47,11 +47,6 @@

<PreferenceCategory android:title="@string/settings_debug">

<SwitchPreference
android:defaultValue="false"
android:key="DebugIcon"
android:title="@string/settings_debug_icon" />

<Preference
android:key="key_get_log"
android:summary="@string/settings_get_log_summary"
Expand Down
4 changes: 4 additions & 0 deletions common/src/main/java/top/trumeet/common/cache/IconCache.java
Expand Up @@ -36,6 +36,10 @@ public static IconCache getInstance() {
return cache;
}

public Bitmap getRawIconBitmapWithoutLoader(final Context ctx, final String pkg) {
return mIconMemoryCaches.get("raw_" + pkg);
}

public Bitmap getRawIconBitmap(final Context ctx, final String pkg) {
return new AbstractCacheAspect<Bitmap>(mIconMemoryCaches) {
@Override
Expand Down
@@ -1,4 +1,4 @@
package com.xiaomi.helper;
package top.trumeet.common.ita;

import android.support.annotation.IntDef;
import android.support.annotation.RestrictTo;
Expand Down
@@ -1,4 +1,4 @@
package com.xiaomi.helper;
package top.trumeet.common.ita;

import android.accessibilityservice.AccessibilityService;
import android.app.Service;
Expand All @@ -10,8 +10,6 @@
*/
public class DetectionService extends AccessibilityService {

final static String TAG = "DetectionService";

static String foregroundPackageName;

public static String getForegroundPackageName() {
Expand All @@ -27,7 +25,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
/**
* 重载辅助功能事件回调函数,对窗口状态变化事件进行处理
*
* @param event
* @param event event
*/
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
Expand Down
@@ -1,4 +1,4 @@
package com.xiaomi.helper;
package top.trumeet.common.ita;

import android.content.Context;

Expand Down
@@ -1,10 +1,10 @@
package com.xiaomi.helper;
package top.trumeet.common.ita;

import android.os.Build;

import com.xiaomi.helper.impl.ActivityAccessibilityImpl;
import com.xiaomi.helper.impl.ActivityUsageStatsImpl;
import com.xiaomi.helper.impl.FakeImpl;
import top.trumeet.common.ita.impl.ActivityAccessibilityImpl;
import top.trumeet.common.ita.impl.ActivityUsageStatsImpl;
import top.trumeet.common.ita.impl.FakeImpl;

/**
* Created by zts1993 on 2018/2/18.
Expand Down
@@ -1,20 +1,20 @@
package com.xiaomi.helper.impl;
package top.trumeet.common.ita.impl;

import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.util.Log;

import com.xiaomi.helper.DetectionService;
import com.xiaomi.helper.ITopActivity;
import top.trumeet.common.ita.DetectionService;
import top.trumeet.common.ita.ITopActivity;

import me.pqpo.librarylog4a.Log4a;

/**
* Created by zts1993 on 2018/2/18.
*/

public class ActivityAccessibilityImpl implements ITopActivity {
final static String TAG = "ActivityAccessibilityImpl";
private final static String TAG = "ActivityAccessibility";

@Override
public boolean isEnabled(Context context) {
Expand All @@ -23,7 +23,7 @@ public boolean isEnabled(Context context) {
accessibilityEnabled = Settings.Secure.getInt(context.getContentResolver(),
Settings.Secure.ACCESSIBILITY_ENABLED);
} catch (Settings.SettingNotFoundException e) {
Log4a.e(TAG, e.getMessage(), e);
Log.e(TAG, e.getMessage(), e);
}

if (accessibilityEnabled == 1) {
Expand Down
@@ -1,4 +1,4 @@
package com.xiaomi.helper.impl;
package top.trumeet.common.ita.impl;

import android.app.ActivityManager;
import android.app.AppOpsManager;
Expand All @@ -7,11 +7,14 @@
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.provider.Settings;
import android.support.annotation.RequiresPermission;
import android.util.Log;
import android.widget.Toast;

import com.xiaomi.helper.ITopActivity;
import top.trumeet.common.ita.ITopActivity;

import java.util.Objects;

import me.pqpo.librarylog4a.Log4a;
import top.trumeet.common.override.ActivityManagerOverride;

import static android.content.Context.ACTIVITY_SERVICE;
Expand All @@ -21,18 +24,19 @@
*/

public class ActivityUsageStatsImpl implements ITopActivity {
static final String TAG = "ActivityUsageStatsImpl";
private static final String TAG = "ActivityUsageStatsImpl";

@Override
public boolean isEnabled(Context context) {
try {
PackageManager packageManager = context.getPackageManager();
ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
return appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS,

ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
return Objects.requireNonNull(appOpsManager).checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS,
applicationInfo.uid, applicationInfo.packageName) == AppOpsManager.MODE_ALLOWED;
} catch (Exception e) {
//ignore
Log.e(TAG, e.getMessage(), e);
}

return false;
Expand All @@ -46,11 +50,11 @@ public void guideToEnable(Context context) {
}

@Override
@RequiresPermission("android.permission.PACKAGE_USAGE_STATS")
public boolean isAppForeground(Context context, String packageName) {
try {
int level = ActivityManagerOverride.getPackageImportance(packageName,
((ActivityManager) context.getSystemService(ACTIVITY_SERVICE)));
Log4a.d(TAG, "Importance flag: " + level);
((ActivityManager) Objects.requireNonNull(context.getSystemService(ACTIVITY_SERVICE))));
return level == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;

} catch (RuntimeException e) {
Expand Down
@@ -1,15 +1,14 @@
package com.xiaomi.helper.impl;
package top.trumeet.common.ita.impl;

import android.content.Context;

import com.xiaomi.helper.ITopActivity;
import top.trumeet.common.ita.ITopActivity;

/**
* Created by zts1993 on 2018/2/18.
*/

public class FakeImpl implements ITopActivity {
static final String TAG = "FakeImpl";

@Override
public boolean isEnabled(Context context) {
Expand Down
Expand Up @@ -16,7 +16,6 @@ public class PreferencesUtils {
public static final String MAIN_PREFS = "top.trumeet.mipush_preferences";

public static final String KEY_ACCESS_MODE = "AccessMode";
public static final String KEY_DEBUG_ICON = "DebugIcon";
public static final String AUTO_REGISTER = "AutoRegister";
public static final String KEY_FOREGROUND_NOTIFICATION = "ForegroundNotification";
private static SharedPreferences mPreference;
Expand Down
2 changes: 1 addition & 1 deletion push/src/main/AndroidManifest.xml
Expand Up @@ -193,7 +193,7 @@
android:exported="true" />

<service
android:name="com.xiaomi.helper.DetectionService"
android:name="top.trumeet.common.ita.DetectionService"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
Expand Down

0 comments on commit 909777f

Please sign in to comment.