xref: /MusicFree/android/app/src/main/java/fun/upup/musicfree/lyricUtil/LyricUtilModule.kt (revision fe4a38198982f78ed0ab998adbf731ec51346ef2)
1 package `fun`.upup.musicfree.lyricUtil
2 
3 import android.app.Activity
4 import android.content.Intent
5 import android.net.Uri
6 import android.os.Build
7 import android.provider.Settings
8 import android.util.Log
9 import androidx.annotation.RequiresApi
10 import com.facebook.react.bridge.*
11 import java.util.*
12 
13 class LyricUtilModule(private val reactContext: ReactApplicationContext): ReactContextBaseJavaModule(reactContext) {
getNamenull14     override fun getName() = "LyricUtil"
15     private var lyricView: LyricView? = null
16 
17     @ReactMethod
18     fun checkSystemAlertPermission(promise: Promise) {
19         try {
20             promise.resolve(Settings.canDrawOverlays(reactContext))
21         } catch (e: Exception) {
22             promise.reject("Error", e.message)
23         }
24     }
25 
26     @ReactMethod
requestSystemAlertPermissionnull27     fun requestSystemAlertPermission(promise: Promise) {
28         try {
29             val intent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION).apply {
30                 data = Uri.parse("package:" + reactContext.packageName)
31             }
32             currentActivity?.startActivity(intent)
33             promise.resolve(true)
34         } catch (e: Exception) {
35             promise.reject("Error", e.message)
36         }
37     }
38 
39     @ReactMethod
showStatusBarLyricnull40     fun showStatusBarLyric(initLyric: String?, options: ReadableMap?, promise: Promise) {
41         try {
42             UiThreadUtil.runOnUiThread {
43                 if (lyricView == null) {
44                     lyricView = LyricView(reactContext)
45                 }
46 
47                 val mapOptions = mutableMapOf<String, Any>().apply {
48                     if (options == null) {
49                         return@apply
50                     }
51                     if (options.hasKey("topPercent")) {
52                         put("topPercent", options.getDouble("topPercent"))
53                     }
54                     if (options.hasKey("leftPercent")) {
55                         put("leftPercent", options.getDouble("leftPercent"))
56                     }
57                     if (options.hasKey("align")) {
58                         put("align", options.getInt("align"))
59                     }
60                     if (options.hasKey("color")) {
61                         options.getString("color")?.let { put("color", it) }
62                     }
63                     if (options.hasKey("backgroundColor")) {
64                         options.getString("backgroundColor")?.let { put("backgroundColor", it) }
65                     }
66                     if (options.hasKey("widthPercent")) {
67                         put("widthPercent", options.getDouble("widthPercent"))
68                     }
69                     if (options.hasKey("fontSize")) {
70                         put("fontSize", options.getDouble("fontSize"))
71                     }
72                 }
73 
74                 try {
75                     lyricView?.showLyricWindow(initLyric, mapOptions)
76                     promise.resolve(true)
77                 } catch (e: Exception) {
78                     promise.reject("Exception", e.message)
79                 }
80             }
81         } catch (e: Exception) {
82             promise.reject("Exception", e.message)
83         }
84     }
85 
86     @ReactMethod
hideStatusBarLyricnull87     fun hideStatusBarLyric(promise: Promise) {
88         try {
89             UiThreadUtil.runOnUiThread {
90                 lyricView?.hideLyricWindow()
91             }
92             promise.resolve(true)
93         } catch (e: Exception) {
94             promise.reject("Exception", e.message)
95         }
96     }
97 
98     @ReactMethod
setStatusBarLyricTextnull99     fun setStatusBarLyricText(lyric: String, promise: Promise) {
100         try {
101             UiThreadUtil.runOnUiThread {
102                 lyricView?.setText(lyric)
103             }
104             promise.resolve(true)
105         } catch (e: Exception) {
106             promise.reject("Exception", e.message)
107         }
108     }
109 
110     @ReactMethod
setStatusBarLyricAlignnull111     fun setStatusBarLyricAlign(alignment: Int, promise: Promise) {
112         try {
113             UiThreadUtil.runOnUiThread {
114                 lyricView?.setAlign(alignment)
115             }
116             promise.resolve(true)
117         } catch (e: Exception) {
118             promise.reject("Exception", e.message)
119         }
120     }
121 
122     @ReactMethod
setStatusBarLyricTopnull123     fun setStatusBarLyricTop(pct: Double, promise: Promise) {
124         try {
125             UiThreadUtil.runOnUiThread {
126                 lyricView?.setTopPercent(pct)
127             }
128             promise.resolve(true)
129         } catch (e: Exception) {
130             promise.reject("Exception", e.message)
131         }
132     }
133 
134     @ReactMethod
setStatusBarLyricLeftnull135     fun setStatusBarLyricLeft(pct: Double, promise: Promise) {
136         try {
137             UiThreadUtil.runOnUiThread {
138                 lyricView?.setLeftPercent(pct)
139             }
140             promise.resolve(true)
141         } catch (e: Exception) {
142             promise.reject("Exception", e.message)
143         }
144     }
145 
146     @ReactMethod
setStatusBarLyricWidthnull147     fun setStatusBarLyricWidth(pct: Double, promise: Promise) {
148         try {
149             UiThreadUtil.runOnUiThread {
150                 lyricView?.setWidth(pct)
151             }
152             promise.resolve(true)
153         } catch (e: Exception) {
154             promise.reject("Exception", e.message)
155         }
156     }
157 
158     @ReactMethod
setStatusBarLyricFontSizenull159     fun setStatusBarLyricFontSize(fontSize: Float, promise: Promise) {
160         try {
161             UiThreadUtil.runOnUiThread {
162                 lyricView?.setFontSize(fontSize)
163             }
164             promise.resolve(true)
165         } catch (e: Exception) {
166             promise.reject("Exception", e.message)
167         }
168     }
169 
170     @ReactMethod
setStatusBarColorsnull171     fun setStatusBarColors(textColor: String?, backgroundColor: String?, promise: Promise) {
172         try {
173             UiThreadUtil.runOnUiThread {
174                 lyricView?.setColors(textColor, backgroundColor)
175             }
176             promise.resolve(true)
177         } catch (e: Exception) {
178             promise.reject("Exception", e.message)
179         }
180     }
181 
182 }
183