Skip to content

Commit

Permalink
更新单例模式
Browse files Browse the repository at this point in the history
  • Loading branch information
tignioj committed Jun 14, 2020
1 parent 29ec2a3 commit 1c460d5
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 38 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package="com.tignioj.freezeapp">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.VIBRATE" />

<application
android:name=".InitApplication"
Expand Down
19 changes: 16 additions & 3 deletions app/src/main/java/com/tignioj/freezeapp/MyDeviceAdminReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;

Expand All @@ -21,6 +23,7 @@ public void onEnabled(Context context, Intent intent) {
Toast.makeText(context, "设备管理:可用", Toast.LENGTH_SHORT).show();
ComponentName componentName = new ComponentName(context, MainActivity.class);

//如果隐藏了App,将它复原
Log.d(MyConfig.LOG_TAG_MyDeviceAdminReceiver, componentName.getPackageName() + ":" + componentName.getClassName() + " enable:" + DeviceMethod.isComponentEnabled(context.getPackageManager(), componentName.getPackageName()
, componentName.getClassName()));
//如果App不小心隐藏了,就将他激活
Expand All @@ -29,9 +32,19 @@ public void onEnabled(Context context, Intent intent) {

private void showApp(Context context) {
PackageManager p = context.getPackageManager();
ComponentName componentName = new ComponentName("com.tignioj.freezeapp", "com.tignioj.freezeapp.MainActivity");
p.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
Log.d(MyConfig.LOG_TAG_MyDeviceAdminReceiver, "receive enable!");
// ComponentName componentName = new ComponentName("com.tignioj.freezeapp", "com.tignioj.freezeapp.MainActivity");
// p.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
// Log.d(MyConfig.LOG_TAG_MyDeviceAdminReceiver, "receive enable!");

ComponentName componentName = new ComponentName(context, MainActivity.class);
if (!DeviceMethod.isComponentEnabled(p, componentName.getPackageName(), componentName.getClassName())) {
Log.d(MyConfig.LOG_TAG_MyBootReceiver, componentName.getPackageName() + ":" + componentName.getClassName() + "冻结了,正在解冻中");
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor edit = mPrefs.edit();
edit.putBoolean(MyConfig.PERSONAL_SHP_CONFIG_KEY_EDITABLE_ENABLE, false);
edit.apply();
p.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ public class AppsCategoryRepository {
LiveData<List<AppsCategory>> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ public class FreezeTaskerRepository {
private static FreezeTaskerRepository INSTANCE;
LiveData<List<FreezeTasker>> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
68 changes: 41 additions & 27 deletions app/src/main/java/com/tignioj/freezeapp/service/FreezeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -90,20 +93,9 @@ public static Handler getHideMySelfHandler() {
MutableLiveData<ConcurrentHashMap<String, String>> notificationHashMapLive;
ConcurrentHashMap<String, String> notificationHashMap;

ReentrantLock reentrantLock;


public class ServiceThread extends Thread {
ConcurrentHashMap<Long, Boolean> screenSchedulingMap;




public static final int OPERATION_GET_MAP_STRING = 0x100001;
public static final int OPERATION_CLEAN_MAP = 0x100000;



/**
* 通知,用于显示当前执行的任务描述
*/
Expand All @@ -114,17 +106,15 @@ public class ServiceThread extends Thread {
isActuallyEnable = DeviceMethod.getInstance(getApplicationContext()).isSelfEnable();
notificationHashMapLive = new MutableLiveData<>();


notificationHashMapLive.observeForever(new Observer<ConcurrentHashMap<String, String>>() {
@Override
public synchronized void onChanged(ConcurrentHashMap<String, String> 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() + "";
Expand Down Expand Up @@ -154,12 +144,34 @@ public synchronized void onChanged(ConcurrentHashMap<String, String> 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
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -239,7 +250,6 @@ private void loopTasks() {
if (notificationHashMap.containsKey(freezeTasker.getDescription())) {
notificationHashMap.remove(freezeTasker.getDescription());
hideMySelfHandler.sendEmptyMessage(UPDATE_NOTIFICATION);

}
processUnLockScreen(freezeTasker);
}
Expand Down Expand Up @@ -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);
Expand All @@ -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<ProgramLocker> programLockerMutableLiveData = homeViewModel.getProgramLockerMutableLiveData();
programLockerMutableLiveData.observeForever(new Observer<ProgramLocker>() {
Expand All @@ -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());


Expand Down Expand Up @@ -422,11 +435,12 @@ public void onChanged(List<FreezeTasker> 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);
}

}
Expand Down

0 comments on commit 1c460d5

Please sign in to comment.