Skip to content

Commit

Permalink
全新的切换上一应用模式
Browse files Browse the repository at this point in the history
  • Loading branch information
fg607 committed Sep 16, 2018
1 parent 0f23820 commit 25751b8
Show file tree
Hide file tree
Showing 11 changed files with 15 additions and 230 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
minSdkVersion 18
targetSdkVersion 28

versionCode 34
versionName "3.0.4.2"
versionCode 35
versionName "3.0.4.3"
}

signingConfigs {
Expand Down
2 changes: 1 addition & 1 deletion app/release/output.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":33,"versionName":"3.0.4.1","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}]
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":34,"versionName":"3.0.4.2","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}]
3 changes: 0 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
<uses-permission
android:name="android.permission.PACKAGE_USAGE_STATS"
tools:ignore="ProtectedPermissions" />

<!-- 常见桌面权限 -->
<uses-permission android:name="com.google.android.apps.nexuslauncher.permission.READ_SETTINGS" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ public void developerInfo() {
dialog.setTitle("关于悬浮助手");
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
dialog.setMessage("版本:3.0.4.2\r\n作者:fg607\r\n邮箱:[email protected]");
dialog.setMessage("版本:3.0.4.3\r\n作者:fg607\r\n邮箱:[email protected]");
dialog.show();
}

Expand Down Expand Up @@ -463,11 +463,14 @@ public void questionsAnswer() {
public void showUpdateInfo() {

AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.setTitle("悬浮助手-3.0.4.2版本更新内容");
dialog.setTitle("悬浮助手-3.0.4.3版本更新内容");
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
dialog.setMessage("" +
"1.优化快捷菜单半透明背景,覆盖状态栏和导航栏。\r\n"+
"1.全新的切换上一应用模式,速度更快,不再需要“使用情况访问权限”。\r\n"+
"2.修复屏幕旋转权限问题。\r\n"+
"3.优化快捷菜单半透明背景,覆盖状态栏和导航栏。\r\n"+
"4.修复选择切换上一应用手势不能立即生效的问题。"+
"");
dialog.show();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ private void executeAction(String action){
FloatingBallUtils.openRecnetTask(NavAccessibilityService.instance);
break;
case "切换上一个应用":
FloatingBallUtils.previousApp();
FloatingBallUtils.previousApp(NavAccessibilityService.instance);
break;
case "休眠(需要开启锁屏功能)":
FloatingBallUtils.lockScreen();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,6 @@ public static boolean isUsageAccess() {
return true;
}

public static void requestUsageAccessPermission() {

Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {

context.startActivity(intent);

} catch (Exception e) {

Toast.makeText(context, "该ROM不支持切换上一应用功能!", Toast.LENGTH_SHORT).show();
}
}

public static boolean isServiceRunning(Context mContext,String className) {

Expand Down
152 changes: 0 additions & 152 deletions app/src/main/java/com/hardwork/fg607/relaxfinger/utils/AppUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -319,158 +319,6 @@ public static String getFilePath(String packageName){
return null;
}

public static String getPreviousApp() throws Exception {

if(Build.VERSION.SDK_INT>=21){

return getPreviousNew();

}else{

return getPreviousOld();

}
}

//API 21 and above
public static String getPreviousNewN() throws Exception {

//List<String> packageNameList = new ArrayList<>();
Field field = null;
try {
field = ActivityManager.RunningAppProcessInfo.class.getDeclaredField("processState");
} catch (Exception ignored) {
}
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> appList = am.getRunningAppProcesses();
//i=1从第二个检索,因为第一个是当前app
for (int i = 1; i < appList.size(); i++) {
Integer state = null;
try {
state = field.getInt(appList.get(i));
} catch (Exception e) {
}

if (state != null) {
ApplicationInfo info = getApplicationInfoByProcessName(appList.get(i).processName);

if (info != null && !info.packageName.equals("com.hardwork.fg607.relaxfinger")
&& !info.packageName.contains("input")
&& !info.packageName.contains("keyboard")) {

Intent intent = pm.getLaunchIntentForPackage(info.packageName);

if (intent != null) {

//Log.i("background", info.packageName);
return info.packageName;
}

}

}
}

return null;
}

//API below 21
@SuppressWarnings("deprecation")
public static String getPreviousOld() throws Exception {

int count = 0;
String packageName = null;
ActivityManager activity = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> runningTask = activity.getRunningTasks(3);
if (runningTask != null) {

for(ActivityManager.RunningTaskInfo task:runningTask){
ComponentName componentTop = task.topActivity;
packageName = componentTop.getPackageName();

Intent intent = pm.getLaunchIntentForPackage(packageName);
if(intent != null && !packageName.equals("com.hardwork.fg607.relaxfinger")
&& !packageName.contains("input")
&& !packageName.contains("keyboard") && !packageName.contains("launcher")){

count++;

if(count == 2){

return packageName;
}

}

}

}

return null;

}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static String getPreviousNew() {

class RecentUseComparator implements Comparator<UsageStats> {
@Override
public int compare(UsageStats lhs, UsageStats rhs) {
return (lhs.getLastTimeUsed() > rhs.getLastTimeUsed()) ? -1 : (lhs.getLastTimeUsed() == rhs.getLastTimeUsed()) ? 0 : 1;
}
}
RecentUseComparator recentComp = new RecentUseComparator();

UsageStatsManager usageStatsManager =
(UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
long ts = System.currentTimeMillis();
List<UsageStats> queryUsageStats =
usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_BEST, ts-1000*60*60, ts);
if (queryUsageStats == null || queryUsageStats.isEmpty()) {

MyApplication.getMainThreadHandler().post(new Runnable() {
@Override
public void run() {
Toast.makeText(context,"切换上一应用需要查看应用使用情况权限!", Toast.LENGTH_SHORT).show();
}
});

AccessibilityUtil.requestUsageAccessPermission();
return null;
}

Collections.sort(queryUsageStats, recentComp);


String packageName = null;

//i=1因为第一个是当前应用
for(int i = 1;i<queryUsageStats.size();i++){


packageName = queryUsageStats.get(i).getPackageName();

Intent intent = pm.getLaunchIntentForPackage(packageName);

if(intent != null && !packageName.equals("com.hardwork.fg607.relaxfinger")
&& !packageName.contains("input") && !packageName.contains("com.android.systemui")
&& !packageName.contains("keyboard") && !packageName.contains("launcher")){

//Log.i("usage",packageName);
return packageName;
}
}

/* UsageStats recentStats = null;
for (UsageStats usageStats : queryUsageStats) {
if(recentStats == null
|| recentStats.getLastTimeUsed() < usageStats.getLastTimeUsed()){
recentStats = usageStats;
}
}*/

return null;
}

/**
* 根据进程名获取应用信息
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ public static void lockScreen(){
public static void openRecnetTask(AccessibilityService service){

service.performGlobalAction(AccessibilityService.GLOBAL_ACTION_RECENTS);


}

public static void openNotificationBar(AccessibilityService service){
Expand Down Expand Up @@ -246,46 +248,10 @@ public static void volumeDown() {
}


public static void previousApp(){

new Thread(new Runnable() {
@Override
public void run() {

String prePackageName = null;

try {

prePackageName = AppUtils.getPreviousApp();

} catch (Exception e) {
e.printStackTrace();
}

if(prePackageName!=null){

try {
AppUtils.startApplication(prePackageName);
}catch (ActivityNotFoundException e){

e.printStackTrace();
}


}else {

MyApplication.getMainThreadHandler().post(new Runnable() {
@Override
public void run() {

Toast.makeText(MyApplication.getApplication(),"没有更早的应用了!",Toast.LENGTH_SHORT).show();
}
});
}
}
}).start();

public static void previousApp(AccessibilityService service){

service.performGlobalAction(AccessibilityService.GLOBAL_ACTION_RECENTS);
service.performGlobalAction(AccessibilityService.GLOBAL_ACTION_RECENTS);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceFragment;
import android.view.View;
import android.widget.Toast;

import com.afollestad.materialdialogs.MaterialDialog;
import com.hardwork.fg607.relaxfinger.R;
Expand All @@ -19,10 +18,7 @@
import com.hardwork.fg607.relaxfinger.utils.FloatingBallUtils;

import net.grandcentrix.tray.AppPreferences;
import net.grandcentrix.tray.TrayAppPreferences;

import static com.hardwork.fg607.relaxfinger.utils.AccessibilityUtil.isUsageAccess;
import static com.hardwork.fg607.relaxfinger.utils.AccessibilityUtil.requestUsageAccessPermission;


public class GestureFragment extends PreferenceFragment implements OnPreferenceClickListener {
Expand Down Expand Up @@ -140,15 +136,6 @@ public void showFunctionDialog(final Preference preference){
@Override
public boolean onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {

if(text.equals("切换上一个应用")){

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && !isUsageAccess()){
Toast.makeText(getActivity(),"切换上一应用需要打开通知使用权限!", Toast.LENGTH_SHORT).show();
requestUsageAccessPermission();
return false;
}
}

switch (preference.getKey()) {

case "click":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -572,9 +572,6 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
}

break;
/*case "autoStartSwitch":
autoStartChange((boolean) newValue);
break;*/
case "autoHideSwitch":
autoHideChange((boolean) newValue);
break;
Expand Down

0 comments on commit 25751b8

Please sign in to comment.