Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Sign in / Register
Toggle navigation
L
location share white
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wanglei
location share white
Commits
5a27f3c3
Commit
5a27f3c3
authored
Oct 24, 2024
by
周文华
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
【新增】新增FCM相关部分逻辑
parent
94a3bfa0
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
322 additions
and
0 deletions
+322
-0
AndroidManifest.xml
app/src/main/AndroidManifest.xml
+8
-0
BatteryStatusReceiver.kt
.../com/base/locationsharewhite/fcm/BatteryStatusReceiver.kt
+43
-0
FCMManager.java
...main/java/com/base/locationsharewhite/fcm/FCMManager.java
+49
-0
MessagingService.java
...ava/com/base/locationsharewhite/fcm/MessagingService.java
+23
-0
MsgMgr.kt
app/src/main/java/com/base/locationsharewhite/fcm/MsgMgr.kt
+28
-0
PackageStatusReceiver.kt
.../com/base/locationsharewhite/fcm/PackageStatusReceiver.kt
+38
-0
ScreenStatusReceiver.java
...com/base/locationsharewhite/fcm/ScreenStatusReceiver.java
+73
-0
TimerManager.java
...in/java/com/base/locationsharewhite/fcm/TimerManager.java
+60
-0
No files found.
app/src/main/AndroidManifest.xml
View file @
5a27f3c3
...
...
@@ -112,6 +112,14 @@
<meta-data
android:name=
"com.facebook.sdk.ApplicationId"
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>
</manifest>
\ No newline at end of file
app/src/main/java/com/base/locationsharewhite/fcm/BatteryStatusReceiver.kt
0 → 100644
View file @
5a27f3c3
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
app/src/main/java/com/base/locationsharewhite/fcm/FCMManager.java
0 → 100644
View file @
5a27f3c3
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
{
}
}
});
}
}
app/src/main/java/com/base/locationsharewhite/fcm/MessagingService.java
0 → 100644
View file @
5a27f3c3
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
app/src/main/java/com/base/locationsharewhite/fcm/MsgMgr.kt
0 → 100644
View file @
5a27f3c3
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
app/src/main/java/com/base/locationsharewhite/fcm/PackageStatusReceiver.kt
0 → 100644
View file @
5a27f3c3
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
app/src/main/java/com/base/locationsharewhite/fcm/ScreenStatusReceiver.java
0 → 100644
View file @
5a27f3c3
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
app/src/main/java/com/base/locationsharewhite/fcm/TimerManager.java
0 → 100644
View file @
5a27f3c3
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment