Commit 5a27f3c3 authored by 周文华's avatar 周文华

【新增】新增FCM相关部分逻辑

parent 94a3bfa0
...@@ -112,6 +112,14 @@ ...@@ -112,6 +112,14 @@
<meta-data <meta-data
android:name="com.facebook.sdk.ApplicationId" android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id" /> android:value="@string/facebook_app_id" />
<service
android:name=".fcm.MessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application> </application>
</manifest> </manifest>
\ No newline at end of file
package com.base.locationsharewhite.fcm
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.BatteryManager
import android.os.Build
/**
*电量监听
*/
class BatteryStatusReceiver : BroadcastReceiver() {
companion object {
fun registerBatteryReceiver(context: Context) {
val intentFilter = IntentFilter().apply {
addAction(Intent.ACTION_BATTERY_CHANGED)
}
val applicationContext = context.applicationContext
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
applicationContext.registerReceiver(
BatteryStatusReceiver(),
intentFilter,
Context.RECEIVER_EXPORTED
)
} else {
applicationContext.registerReceiver(BatteryStatusReceiver(), intentFilter)
}
}
}
override fun onReceive(context: Context?, intent: Intent?) {
val action = intent?.action
if (action == Intent.ACTION_BATTERY_CHANGED) {
val batteryLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)
val batteryScale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1)
val batteryPercentage = (batteryLevel / batteryScale.toFloat()) * 100
}
}
}
\ No newline at end of file
package com.base.locationsharewhite.fcm;
import android.content.Context;
import android.util.Log;
import androidx.annotation.NonNull;
import com.base.locationsharewhite.utils.LogEx;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseApp;
import com.google.firebase.messaging.FirebaseMessaging;
public class FCMManager {
public static void initFirebase(Context context) {
FirebaseApp.initializeApp(context);
}
public static void subscribeToTopic(String topic) {
FirebaseMessaging.getInstance().subscribeToTopic(topic)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
LogEx.INSTANCE.logDebug("FCMUtil", "suc:" + topic, false);
// EventUtils.INSTANCE.event("FCM_Topic_" + topic, null, null, false);
} else {
Log.d("FCMUtil", "fail");
}
}
});
}
public static void unsubscribeFromTopic(String topic) {
FirebaseMessaging.getInstance().unsubscribeFromTopic(topic)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
} else {
}
}
});
}
}
package com.base.locationsharewhite.fcm;
import android.annotation.SuppressLint;
import androidx.annotation.NonNull;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
@SuppressLint("MissingFirebaseInstanceTokenRefresh")
public class MessagingService extends FirebaseMessagingService {
private static final String TAG = "MessagingService";
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
}
}
\ No newline at end of file
package com.base.locationsharewhite.fcm
import android.content.Context
import com.base.locationsharewhite.helper.ConfigHelper
import com.base.locationsharewhite.utils.AppPreferences
import com.base.locationsharewhite.utils.KotlinExt.toFormatMinute
object MsgMgr {
/**
* FCM主题订阅 Topic 包名+首次启动的当前的分钟
*/
private var topic
get() = AppPreferences.getInstance().getString("topic", "")
set(value) = AppPreferences.getInstance().put("topic", value)
fun init(context: Context) {
FCMManager.initFirebase(context)
var topic = this.topic
if (topic.isEmpty()) {
val topicNumber = System.currentTimeMillis().toFormatMinute()
topic = ConfigHelper.packageName + "_$topicNumber"
this.topic = topic
}
FCMManager.subscribeToTopic(topic)
}
}
\ No newline at end of file
package com.base.locationsharewhite.fcm
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Build
class PackageStatusReceiver : BroadcastReceiver() {
companion object {
fun registerBatteryReceiver(context: Context) {
val intentFilter = IntentFilter().apply {
addAction(Intent.ACTION_PACKAGE_ADDED)
addAction(Intent.ACTION_PACKAGE_REMOVED)
addDataScheme("package")
}
val applicationContext = context.applicationContext
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
applicationContext.registerReceiver(
PackageStatusReceiver(),
intentFilter,
Context.RECEIVER_EXPORTED
)
} else {
applicationContext.registerReceiver(PackageStatusReceiver(), intentFilter)
}
}
}
override fun onReceive(context: Context?, intent: Intent?) {
val action = intent?.action
if (action == Intent.ACTION_PACKAGE_ADDED || action == Intent.ACTION_PACKAGE_REMOVED) {
}
}
}
\ No newline at end of file
package com.base.locationsharewhite.fcm;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import com.base.locationsharewhite.utils.AppPreferences;
import java.util.Objects;
public class ScreenStatusReceiver extends BroadcastReceiver {
private static boolean isDeviceInteractive = true;
private static boolean isSecureLockActive = false;
public static void setupScreenStatusListener(Context context) {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
intentFilter.addAction(Intent.ACTION_SCREEN_ON);
intentFilter.addAction(Intent.ACTION_USER_PRESENT);
final Context applicationContext = context.getApplicationContext();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
applicationContext.registerReceiver(new ScreenStatusReceiver(), intentFilter, Context.RECEIVER_EXPORTED);
} else {
applicationContext.registerReceiver(new ScreenStatusReceiver(), intentFilter);
}
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (Objects.requireNonNull(action)) {
case Intent.ACTION_SCREEN_ON:
setDeviceInteractive(true);
break;
case Intent.ACTION_SCREEN_OFF:
setDeviceInteractive(false);
setSecureLockActive(true);
break;
case Intent.ACTION_USER_PRESENT:
setSecureLockActive(false);
if (isDeviceInteractive() && !isSecureLockActive()) {
int secureSetting = Integer.parseInt(AppPreferences.getInstance().getString("lockS", "1"));
if (secureSetting == 1) {
// String actionId = NotificationUiUtil.INSTANCE.getNextActionId();
// NotificationUiUtil.INSTANCE.sendNotificationIfCan(context, actionId, PopupConstObject.POPUP_WHERE_LOCK);
}
}
break;
}
}
private void setDeviceInteractive(boolean interactive) {
isDeviceInteractive = interactive;
}
public static boolean isDeviceInteractive() {
return isDeviceInteractive;
}
private void setSecureLockActive(boolean active) {
isSecureLockActive = active;
}
public static boolean isSecureLockActive() {
return isSecureLockActive;
}
}
\ No newline at end of file
package com.base.locationsharewhite.fcm;
import android.util.Log;
import java.util.Timer;
import java.util.TimerTask;
public class TimerManager {
private static TimerManager instance;
private Timer taskTimer;
private boolean isTimerActive;
private TimerManager() {
// 私有构造方法
}
public static synchronized TimerManager getInstance() {
if (instance == null) {
instance = new TimerManager();
}
return instance;
}
public void scheduleTask(long delay, long period) {
synchronized (TimerManager.class) {
ensureTimerIsStopped(); // 确保定时器未运行
taskTimer = new Timer(); // 创建新的 Timer 实例
TimerTask task = new TimerTask() {
@Override
public void run() {
Log.d("glc", "Scheduled task is running");
// 确保设备处于交互状态,未锁定,且应用未暂停
}
};
taskTimer.schedule(task, delay, period); // 调度任务
isTimerActive = true; // 设置定时器状态为活跃
}
}
private void ensureTimerIsStopped() {
if (isTimerActive) {
if (taskTimer != null) {
taskTimer.cancel();
taskTimer.purge(); // 清除所有取消的任务
}
isTimerActive = false; // 重置定时器状态
}
}
public void stopTaskTimer() {
synchronized (TimerManager.class) {
ensureTimerIsStopped(); // 停止定时器
}
}
public boolean isTaskTimerActive() {
return isTimerActive;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment