version:2.0

fix:
update:增加角标
This commit is contained in:
2022-06-06 22:05:30 +08:00
parent d7c2bf135a
commit 190810bdc0
10 changed files with 366 additions and 64 deletions

View File

@@ -1,15 +1,18 @@
package com.uiui.aios.activity.main;
import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.IBinder;
import android.os.RemoteException;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
@@ -32,8 +35,10 @@ import com.uiui.aios.fragment.AppListFragment;
import com.uiui.aios.fragment.BaseFragmentPagerAdapter;
import com.uiui.aios.fragment.CustomFragment;
import com.uiui.aios.fragment.SecondFragment;
import com.uiui.aios.service.NotificationService;
import com.uiui.aios.utils.ApkUtils;
import com.uiui.aios.utils.AppUsedTimeUtils;
import com.uiui.aios.utils.ToastUtil;
import com.uiui.aios.view.ScaleCircleNavigator;
import net.lucode.hackware.magicindicator.MagicIndicator;
@@ -70,6 +75,7 @@ public class MainActivity extends BaseActivity implements MainContact.MainView {
@Override
public void initView() {
ButterKnife.bind(this);
toggleNotificationListenerService(this);
mMainPresenter = new MainPresenter(this);
mMainPresenter.attachView(this);
mMainPresenter.setLifecycle(lifecycleSubject);
@@ -158,6 +164,16 @@ public class MainActivity extends BaseActivity implements MainContact.MainView {
}
}
public static void toggleNotificationListenerService(Context context) {
Log.e(TAG, "toggleNotificationListenerService");
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(new ComponentName(context, NotificationService.class),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
pm.setComponentEnabledSetting(new ComponentName(context, NotificationService.class),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
}
private void getAlarmData() {
if (mIAlarmAidlInterface == null) {
bindAlarmService();
@@ -251,9 +267,57 @@ public class MainActivity extends BaseActivity implements MainContact.MainView {
@Override
protected void onResume() {
super.onResume();
if (!isNotificationListenersEnabled()) {
ToastUtil.show("请授予\"" + getString(R.string.app_name) + "\"使用通知权");
gotoNotificationAccessSetting(this);
}
mMainPresenter.sendRunningInfo();
}
private static final String ENABLED_NOTIFICATION_LISTENERS = "enabled_notification_listeners";
private boolean isNotificationListenersEnabled() {
String pkgName = getPackageName();
final String flat = Settings.Secure.getString(getContentResolver(), ENABLED_NOTIFICATION_LISTENERS);
if (!TextUtils.isEmpty(flat)) {
final String[] names = flat.split(":");
for (int i = 0; i < names.length; i++) {
final ComponentName cn = ComponentName.unflattenFromString(names[i]);
if (cn != null) {
if (TextUtils.equals(pkgName, cn.getPackageName())) {
return true;
}
}
}
}
return false;
}
public static boolean gotoNotificationAccessSetting(Context context) {
try {
Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
return true;
} catch (ActivityNotFoundException e) {//普通情况下找不到的时候需要再特殊处理找一次
try {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.Settings$NotificationAccessSettingsActivity");
intent.setComponent(cn);
intent.putExtra(":settings:show_fragment", "NotificationAccessSettings");
context.startActivity(intent);
return true;
} catch (Exception e1) {
e1.printStackTrace();
}
ToastUtil.show("对不起,您的手机暂不支持");
e.printStackTrace();
return false;
}
}
@Override
protected void onRestart() {
super.onRestart();

View File

@@ -9,11 +9,13 @@ import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;
import com.uiui.aios.R;
import com.uiui.aios.bean.AppListInfo;
import com.uiui.aios.service.NotificationService;
import com.uiui.aios.utils.ApkUtils;
import java.util.List;
@@ -46,14 +48,25 @@ public class APPListAdapter extends RecyclerView.Adapter<APPListAdapter.holder>
@Override
public void onBindViewHolder(@NonNull holder holder, int position) {
final AppListInfo info = mAppListInfos.get(position);
String pkg = info.getPackageName();
holder.icon.setImageBitmap(info.getIcon());
holder.appname.setText(info.getAppName());
holder.root.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ApkUtils.openPackage(mContext, info.getPackageName());
ApkUtils.openPackage(mContext, pkg);
}
});
int size = NotificationService.getNotificationLength(pkg);
if (size == 0) {
holder.bg.setVisibility(View.GONE);
} else if (size < 99) {
holder.bg.setVisibility(View.VISIBLE);
holder.bg.setText(String.valueOf(size));
} else {
holder.bg.setVisibility(View.VISIBLE);
holder.bg.setText("99+");
}
}
@Override
@@ -64,12 +77,14 @@ public class APPListAdapter extends RecyclerView.Adapter<APPListAdapter.holder>
class holder extends RecyclerView.ViewHolder {
private ImageView icon;
private TextView appname;
private LinearLayout root;
private TextView bg;
private ConstraintLayout root;
public holder(@NonNull View itemView) {
super(itemView);
icon = itemView.findViewById(R.id.iv_icon);
appname = itemView.findViewById(R.id.tv_appname);
bg = itemView.findViewById(R.id.bg);
root = itemView.findViewById(R.id.root);
}

View File

@@ -6,6 +6,7 @@ import android.content.res.Configuration;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import android.util.Log;
@@ -21,6 +22,7 @@ import com.uiui.aios.R;
import com.uiui.aios.bean.BaseResponse;
import com.uiui.aios.bean.DesktopIcon;
import com.uiui.aios.network.NetInterfaceManager;
import com.uiui.aios.service.NotificationService;
import com.uiui.aios.utils.ApkUtils;
import com.uiui.aios.utils.BitmapUtils;
import com.uiui.aios.utils.IconUtils;
@@ -115,17 +117,28 @@ public class AppListFragment extends Fragment {
null);
ImageView iv = view.findViewById(R.id.iv);
TextView tv = view.findViewById(R.id.tv);
LinearLayout linearLayout = view.findViewById(R.id.btn_booktag);
TextView bg = view.findViewById(R.id.bg);
ConstraintLayout constraintLayout = view.findViewById(R.id.btn_booktag);
DesktopIcon desktopIcon = mDesktopIcons.get(index);
if (desktopIcon != null) {
Log.e(TAG, "getView: " + desktopIcon.getPackageName());
int i = IconUtils.appClassNameList.indexOf(desktopIcon.getPackageName());
String pkg =desktopIcon.getPackageName();
Log.e(TAG, "getView: " + pkg);
int i = IconUtils.appClassNameList.indexOf(pkg);
if (i != -1) {
String val = IconUtils.appIconList.get(i);
int resID = getActivity().getResources().getIdentifier(val, "drawable", "com.uiui.aios");
int size = NotificationService.getNotificationLength(pkg);
if (size == 0) {
bg.setVisibility(View.GONE);
} else if (size < 99) {
bg.setVisibility(View.VISIBLE);
bg.setText(String.valueOf(size));
} else {
bg.setVisibility(View.VISIBLE);
bg.setText("99+");
}
if (resID == 0) {
Log.e(TAG, "getView: not found src : " + desktopIcon.getPackageName());
Log.e(TAG, "getView: not found src : " + pkg);
iv.setImageDrawable(desktopIcon.getIcon());
} else {
iv.setImageDrawable(getActivity().getResources().getDrawable(resID));

View File

@@ -0,0 +1,89 @@
package com.uiui.aios.service;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* @author TT
*/
public class NotificationService extends NotificationListenerService {
private static final String TAG = NotificationService.class.getSimpleName();
private static HashMap<String, List<StatusBarNotification>> notificationMap = new HashMap<String, List<StatusBarNotification>>();
private static NotificationListener mListener;
private void updateNotification() {
notificationMap.clear();
StatusBarNotification[] statusBarNotifications = getActiveNotifications();
for (StatusBarNotification sbn : statusBarNotifications) {
String pkg = sbn.getPackageName();
Log.e(TAG, "onListenerConnected: " + pkg);
if (notificationMap.get(pkg) == null) {
notificationMap.put(pkg, new ArrayList<>());
notificationMap.get(pkg).add(sbn);
} else {
notificationMap.get(pkg).add(sbn);
}
}
Log.e(TAG, "onListenerConnected: " + notificationMap);
}
public static int getNotificationLength(String pkg) {
if (notificationMap.get(pkg) == null) {
return 0;
} else {
return notificationMap.get(pkg).size();
}
}
public static void setListener(NotificationListener listener) {
mListener = listener;
}
public interface NotificationListener {
void onListenerConnected();
void onNotificationPosted(StatusBarNotification sbn);
void onNotificationRemoved(StatusBarNotification sbn);
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onListenerConnected() {
super.onListenerConnected();
Log.e(TAG, "onListenerConnected: " + getActiveNotifications().length);
updateNotification();
if (mListener != null)
mListener.onListenerConnected();
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
super.onNotificationPosted(sbn);
Log.e(TAG, "onNotificationPosted: " + getActiveNotifications().length);
updateNotification();
if (mListener != null)
mListener.onNotificationPosted(sbn);
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
super.onNotificationRemoved(sbn);
Log.e(TAG, "onNotificationRemoved: " + getActiveNotifications().length);
updateNotification();
if (mListener != null)
mListener.onNotificationRemoved(sbn);
}
}