From 1c460d56c51942a62eba1f2ef343197323f34f08 Mon Sep 17 00:00:00 2001 From: tignioj <859897994@qq.com> Date: Sun, 14 Jun 2020 19:25:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=8D=95=E4=BE=8B=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/AndroidManifest.xml | 1 + .../freezeapp/MyDeviceAdminReceiver.java | 19 +++++- .../repo/AppsCategoryRepository.java | 8 ++- .../viewmodel/repo/FreezeAppRepository.java | 8 ++- .../repo/FreezeTaskerRepository.java | 8 ++- .../viewmodel/repo/HomeRepository.java | 8 ++- .../freezeapp/service/FreezeService.java | 68 +++++++++++-------- 7 files changed, 82 insertions(+), 38 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index b201e1d..bdbdbf4 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -3,6 +3,7 @@ package="com.tignioj.freezeapp"> + > listLiveDataAppsCategory; - public synchronized static AppsCategoryRepository getInstance(Context context) { + public static AppsCategoryRepository getInstance(Context context) { if (INSTANCE == null) { - INSTANCE = new AppsCategoryRepository(context); + synchronized (AppsCategoryRepository.class) { + if (INSTANCE == null) { + INSTANCE = new AppsCategoryRepository(context); + } + } } return INSTANCE; } diff --git a/app/src/main/java/com/tignioj/freezeapp/backend/viewmodel/repo/FreezeAppRepository.java b/app/src/main/java/com/tignioj/freezeapp/backend/viewmodel/repo/FreezeAppRepository.java index 4defb55..06bc7f5 100644 --- a/app/src/main/java/com/tignioj/freezeapp/backend/viewmodel/repo/FreezeAppRepository.java +++ b/app/src/main/java/com/tignioj/freezeapp/backend/viewmodel/repo/FreezeAppRepository.java @@ -16,9 +16,13 @@ public class FreezeAppRepository { private Context mContext; private static FreezeAppRepository INSTANCE; - public synchronized static FreezeAppRepository getInstance(Context context) { + public static FreezeAppRepository getInstance(Context context) { if (INSTANCE == null) { - INSTANCE = new FreezeAppRepository(context); + synchronized (FreezeAppRepository.class) { + if (INSTANCE == null) { + INSTANCE = new FreezeAppRepository(context); + } + } } return INSTANCE; } diff --git a/app/src/main/java/com/tignioj/freezeapp/backend/viewmodel/repo/FreezeTaskerRepository.java b/app/src/main/java/com/tignioj/freezeapp/backend/viewmodel/repo/FreezeTaskerRepository.java index 32e244d..e11629d 100644 --- a/app/src/main/java/com/tignioj/freezeapp/backend/viewmodel/repo/FreezeTaskerRepository.java +++ b/app/src/main/java/com/tignioj/freezeapp/backend/viewmodel/repo/FreezeTaskerRepository.java @@ -17,9 +17,13 @@ public class FreezeTaskerRepository { private static FreezeTaskerRepository INSTANCE; LiveData> listLiveDataFreezeTasker; - public synchronized static FreezeTaskerRepository getInstance(Context context) { + public static FreezeTaskerRepository getInstance(Context context) { if (INSTANCE == null) { - INSTANCE = new FreezeTaskerRepository(context); + synchronized (FreezeTaskerRepository.class) { + if (INSTANCE == null) { + INSTANCE = new FreezeTaskerRepository(context); + } + } } return INSTANCE; } diff --git a/app/src/main/java/com/tignioj/freezeapp/backend/viewmodel/repo/HomeRepository.java b/app/src/main/java/com/tignioj/freezeapp/backend/viewmodel/repo/HomeRepository.java index 8f85464..1866e16 100644 --- a/app/src/main/java/com/tignioj/freezeapp/backend/viewmodel/repo/HomeRepository.java +++ b/app/src/main/java/com/tignioj/freezeapp/backend/viewmodel/repo/HomeRepository.java @@ -44,9 +44,13 @@ public void setProgramLocker(ProgramLocker programLocker) { this.programLockerMutableLiveData.setValue(programLocker); } - public synchronized static HomeRepository getInstance(Context context) { + public static HomeRepository getInstance(Context context) { if (homeRepository == null) { - homeRepository = new HomeRepository(context); + synchronized (HomeRepository.class) { + if (homeRepository == null) { + homeRepository = new HomeRepository(context); + } + } } return homeRepository; } diff --git a/app/src/main/java/com/tignioj/freezeapp/service/FreezeService.java b/app/src/main/java/com/tignioj/freezeapp/service/FreezeService.java index 1dab2fb..25672b5 100644 --- a/app/src/main/java/com/tignioj/freezeapp/service/FreezeService.java +++ b/app/src/main/java/com/tignioj/freezeapp/service/FreezeService.java @@ -5,6 +5,7 @@ import android.app.NotificationManager; import android.app.Service; import android.content.ComponentName; +import android.content.Context; import android.content.Entity; import android.content.Intent; import android.content.IntentFilter; @@ -16,6 +17,8 @@ import android.os.IBinder; import android.os.Looper; import android.os.Message; +import android.os.VibrationEffect; +import android.os.Vibrator; import android.preference.PreferenceManager; import android.util.Log; import android.widget.Toast; @@ -90,20 +93,9 @@ public static Handler getHideMySelfHandler() { MutableLiveData> notificationHashMapLive; ConcurrentHashMap notificationHashMap; - ReentrantLock reentrantLock; - public class ServiceThread extends Thread { ConcurrentHashMap screenSchedulingMap; - - - - - public static final int OPERATION_GET_MAP_STRING = 0x100001; - public static final int OPERATION_CLEAN_MAP = 0x100000; - - - /** * 通知,用于显示当前执行的任务描述 */ @@ -114,17 +106,15 @@ public class ServiceThread extends Thread { isActuallyEnable = DeviceMethod.getInstance(getApplicationContext()).isSelfEnable(); notificationHashMapLive = new MutableLiveData<>(); - notificationHashMapLive.observeForever(new Observer>() { @Override public synchronized void onChanged(ConcurrentHashMap strings) { //拼接内容 + start = null; + end = null; StringBuilder content = new StringBuilder(); if (notificationHashMap == null || notificationHashMap.size() == 0) { content.append("暂时无任务"); - start = null; - end = null; - } else { content.append("["); // text = notificationHashMap.keySet() + ""; @@ -154,12 +144,34 @@ public synchronized void onChanged(ConcurrentHashMap strings) { } else { builder.setContentTitle(null); } + builder.setPriority(NotificationCompat.PRIORITY_DEFAULT); startForeground(NOTIFICATION_ID, builder.build()); + +// long[] pattern = new long[]{1000, 1000, 1000, 1000}; +// myVibrate(getApplicationContext() ,pattern); } }); - notificationHashMap = new ConcurrentHashMap<>(); - notificationHashMapLive.setValue(notificationHashMap); + + } + /** + * 震动 + * @param context + * @param pattern + */ + public void myVibrate(Context context, long[] pattern) { + Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); + if (vibrator == null) { + return; + } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + // -1 : Play exactly once + VibrationEffect effect = VibrationEffect.createWaveform(pattern, -1); +// vibrator.vibrate(VibrationEffect.createOneShot(pattern[0], VibrationEffect.DEFAULT_AMPLITUDE)); + vibrator.vibrate(effect); + } else { + vibrator.vibrate(pattern, -1); + } } @Override @@ -199,7 +211,6 @@ private void loopHideIcon() { if (hideMySelfHandler != null) { hideMySelfHandler.sendEmptyMessage(HIDE_SELF); } - } } else { Log.d(MyConfig.LOG_TAG_FREEZE_SERVICE, "liberate:" + "actually Enable:" + isActuallyEnable); @@ -216,20 +227,20 @@ private void loopTasks() { Log.d(MyConfig.MY_TAG, new Date().toString() + ":循环查看任务, " + (freezeTaskers == null ? null : freezeTaskers.size())); for (FreezeTasker freezeTasker : freezeTaskers) { Log.d(MyConfig.MY_TAG, freezeTasker.toString()); - if (!freezeTasker.isEnable()) { continue; } - //只处理这时间段的App if (MyDateUtils.betweenStartTimeAndEndTime(freezeTasker.getStartTime(), freezeTasker.getEndTime())) { if (!notificationHashMap.containsKey(freezeTasker.getDescription())) { + notificationHashMap.put(freezeTasker.getDescription(), MyDateUtils.format(freezeTasker.getStartTime()) + "-" + MyDateUtils.format(freezeTasker.getEndTime())); - Message message = new Message(); - message.what = UPDATE_NOTIFICATION; - hideMySelfHandler.sendMessage(message); + hideMySelfHandler.sendEmptyMessage(UPDATE_NOTIFICATION); + + + } processLockScreen(freezeTasker); @@ -239,7 +250,6 @@ private void loopTasks() { if (notificationHashMap.containsKey(freezeTasker.getDescription())) { notificationHashMap.remove(freezeTasker.getDescription()); hideMySelfHandler.sendEmptyMessage(UPDATE_NOTIFICATION); - } processUnLockScreen(freezeTasker); } @@ -331,6 +341,7 @@ private void createNotificationChannel() { int importance = NotificationManager.IMPORTANCE_DEFAULT; NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance); channel.setDescription(description); + channel.enableVibration(false); // Register the channel with the system; you can't change the importance // or other notification behaviors after this NotificationManager notificationManager = getSystemService(NotificationManager.class); @@ -348,7 +359,6 @@ public int onStartCommand(Intent intent, int flags, int startId) { public void onCreate() { super.onCreate(); - reentrantLock = new ReentrantLock(); homeViewModel = new HomeViewModel(getApplication()); MutableLiveData programLockerMutableLiveData = homeViewModel.getProgramLockerMutableLiveData(); programLockerMutableLiveData.observeForever(new Observer() { @@ -360,7 +370,10 @@ public void onChanged(ProgramLocker programLocker) { builder = new NotificationCompat.Builder(FreezeService.this.getApplicationContext(), CHANNEL_ID) .setSmallIcon(R.drawable.ic_lock_black_24dp) .setContentTitle(getString(R.string.notifaction_keep_running_title)) + .setPriority(NotificationCompat.PRIORITY_DEFAULT); + createNotificationChannel(); + // startForeground(NOTIFICATION_ID, builder.build()); @@ -422,11 +435,12 @@ public void onChanged(List freezeTaskers) { } // Log.d(MyConfig.LOG_TAG_FREEZE_SERVICE, "clear notification map"); // notificationHashMap.clear(); - hideMySelfHandler.sendEmptyMessage(UPDATE_NOTIFICATION); FreezeService.this.freezeTaskers = freezeTaskers; + + if (serviceThread != null) { notificationHashMap.clear(); - notificationHashMapLive.setValue(notificationHashMap); + hideMySelfHandler.sendEmptyMessage(UPDATE_NOTIFICATION); } }