forked from kvdroid/Kvdroid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
322 lines (274 loc) · 13 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import webbrowser
from kivy.utils import platform
from kivy.logger import Logger
if platform == "android":
try:
from kivy.core.window import Window
from jnius import autoclass, cast, JavaException
from android.runnable import run_on_ui_thread
AndroidActivity = autoclass('android.app.Activity')
PythonActivity = autoclass('org.kivy.android.PythonActivity')
activity = PythonActivity.mActivity
Build = autoclass("android.os.Build")
VERSION = autoclass('android.os.Build$VERSION')
VERSION_CODES = autoclass("android.os.Build$VERSION_CODES")
View = autoclass('android.view.View')
Color = autoclass("android.graphics.Color")
WindowManager = autoclass('android.view.WindowManager$LayoutParams')
Intent = autoclass('android.content.Intent')
Provider = autoclass('android.provider.Settings')
URLConnection = autoclass("java.net.URLConnection")
Toast = autoclass('android.widget.Toast')
Uri = autoclass('android.net.Uri')
Locale = autoclass('java.util.Locale')
ConnectivityManager = autoclass('android.net.ConnectivityManager')
Configuration = autoclass("android.content.res.Configuration")
Environment = autoclass("android.os.Environment")
File = autoclass('java.io.File')
BitmapFactory = autoclass('android.graphics.BitmapFactory')
WallpaperManager = autoclass('android.app.WallpaperManager')
TextToSpeech = autoclass('android.speech.tts.TextToSpeech')
Context = autoclass('android.content.Context')
Request = autoclass("android.app.DownloadManager$Request")
Runtime = autoclass('java.lang.Runtime')
String = autoclass("java.lang.String")
StrictMode = autoclass('android.os.StrictMode')
MediaPlayer = autoclass('android.media.MediaPlayer')
AudioManager = autoclass('android.media.AudioManager')
Rect = autoclass('android.graphics.Rect')()
StatFs = autoclass("android.os.StatFs")
MemoryInfo = autoclass('android.app.ActivityManager$MemoryInfo')
BatteryManager = autoclass("android.os.BatteryManager")
IntentFilter = autoclass("android.content.IntentFilter")
Point = autoclass("android.graphics.Point")
packages = {
"whatsapp": "com.whatsapp",
"facebook": "com.facebook.katana",
"facebookLite": "com.facebook.lite",
"oldFacebook": "com.facebook.android",
"linkedin": "com.linkedin.android",
"fbMessenger": "com.facebook.orca",
"fbMessengerLite": "com.facebook.mlite",
"tiktok": "com.zhiliaoapp.musically",
"tiktokLite": "com.zhiliaoapp.musically.go",
"twitter": "com.twitter.android",
"twitterLite": "com.twitter.android.lite",
"telegram": "org.telegram.messenger",
"telegramX": "org.thunderdog.challegram",
"snapchat": "com.snapchat.android"
}
except BaseException:
Logger.error(
"Kvdroid: Cannot load classes by Pyjnius. Make sure requirements installed"
)
def keyboard_height():
try:
decor_view = activity.getWindow().getDecorView()
height = activity.getWindowManager().getDefaultDisplay().getHeight()
decor_view.getWindowVisibleDisplayFrame(Rect)
return height - Rect.bottom
except:
return 0
def device_info(text,convert = False):
bm = activity.getSystemService(Context.BATTERY_SERVICE)
count = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER)
cap = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY)
intent = activity.registerReceiver(None, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
def convert_bytes(num):
step_unit = 1000.0 #1024 bad the size
for x in ['bytes', 'KB', 'MB', 'GB', 'TB']:
if num < step_unit:
return "%3.1f %s" % (num, x)
num /= step_unit
def avail_mem():
stat = StatFs(Environment.getDataDirectory().getPath())
bytesAvailable = stat.getBlockSize() * stat.getAvailableBlocks()
if convert:
return convert_bytes(bytesAvailable)
else:
return bytesAvailable
def total_mem():
stat = StatFs(Environment.getDataDirectory().getPath())
bytesAvailable = stat.getBlockSize() *stat.getBlockCount()
if convert:
return convert_bytes(bytesAvailable)
else:
return bytesAvailable
def used_mem():
stat = StatFs(Environment.getDataDirectory().getPath())
total = stat.getBlockSize() *stat.getBlockCount()
avail = stat.getBlockSize() * stat.getAvailableBlocks()
if convert:
return convert_bytes(total - avail)
else:
return total - avail
def avail_ram():
memInfo = MemoryInfo()
service = activity.getSystemService(Context.ACTIVITY_SERVICE)
service.getMemoryInfo(memInfo)
if convert:
return convert_bytes(memInfo.availMem)
else:
return memInfo.availMem
def total_ram():
memInfo = MemoryInfo()
service = activity.getSystemService(Context.ACTIVITY_SERVICE)
service.getMemoryInfo(memInfo)
if convert:
return convert_bytes(memInfo.totalMem)
else:
return memInfo.totalMem
def used_ram():
memInfo = MemoryInfo()
service = activity.getSystemService(Context.ACTIVITY_SERVICE)
service.getMemoryInfo(memInfo)
if convert:
return convert_bytes(memInfo.totalMem - memInfo.availMem)
else:
return memInfo.totalMem - memInfo.availMem
os = {
'model': Build.MODEL,
'brand': Build.BRAND,
'manufacturer': Build.MANUFACTURER,
'version': VERSION.RELEASE,
'sdk': VERSION.SDK,
'product': Build.PRODUCT,
'base': VERSION.BASE_OS,
'rom': VERSION.INCREMENTAL,
'security': VERSION.SECURITY_PATCH,
'hardware': Build.HARDWARE,
'tags': Build.TAGS,
'sdk_int': VERSION.SDK_INT,
'cpu_abi': Build.CPU_ABI,
'cpu_cores': Runtime.getRuntime().availableProcessors(),
'avail_mem': avail_mem(),
'total_mem': total_mem(),
'used_mem': used_mem(),
'avail_ram': avail_ram(),
'total_ram': total_ram(),
'used_ram': used_ram(),
'bat_level': bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY),
'bat_capacity': round((count/cap)*100),
'bat_tempeture': intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0) / 10,
'bat_voltage': float(intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE,0) * 0.001),
'bat_technology': intent.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY)
}
return os[text]
@run_on_ui_thread
def immersive_mode(immerse=None):
if immerse:
window = activity.getWindow()
return window.getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
def launch_app(app_package, app_activity):
intent = Intent()
intent.setAction(Intent.ACTION_VIEW)
intent.setClassName(app_package, app_activity)
return activity.startActivity(intent)
def app_details(app_package):
intent = Intent()
intent.setAction(Provider.ACTION_APPLICATION_DETAILS_SETTINGS)
uri = Uri.parse("package:" + app_package)
intent.setData(uri)
activity.startActivity(intent)
@run_on_ui_thread
def statusbar_color(color, text_color):
window = activity.getWindow()
if str(text_color) == "black":
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)
elif str(text_color) == "white":
window.getDecorView().setSystemUiVisibility(0)
else:
raise TypeError("Available options are ['white','black'] for StatusBar text color")
window.clearFlags(WindowManager.FLAG_TRANSLUCENT_STATUS)
window.addFlags(WindowManager.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
window.setStatusBarColor(Color.parseColor(str(color)))
@run_on_ui_thread
def navbar_color(color):
window = activity.getWindow()
window.setNavigationBarColor(Color.parseColor(str(color)))
def toast(message):
return PythonActivity.toastError(str(message))
def set_wallpaper(path_to_image):
context = cast('android.content.Context', activity.getApplicationContext())
file = File(str(path_to_image))
bitmap = BitmapFactory.decodeFile(file.getAbsolutePath())
manager = WallpaperManager.getInstance(context)
return manager.setBitmap(bitmap)
def speech(text, lang):
tts = TextToSpeech(activity, None)
tts.setLanguage(Locale(str(lang)))
return tts.speak(str(text), TextToSpeech.QUEUE_FLUSH, None)
def download_manager(title, description, url, folder, file_name):
uri = Uri.parse(str(url))
dm = cast("android.app.DownloadManager", activity.getSystemService(Context.DOWNLOAD_SERVICE))
request = Request(uri)
request.setTitle(str(title))
request.setDescription(str(description))
request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
request.setDestinationInExternalPublicDir(str(folder), str(file_name))
dm.enqueue(request)
def restart_app(restart=False):
if restart:
currentActivity = cast('android.app.Activity', PythonActivity.mActivity)
context = cast('android.content.Context', currentActivity.getApplicationContext())
packageManager = context.getPackageManager()
intent = packageManager.getLaunchIntentForPackage(context.getPackageName())
componentName = intent.getComponent()
mainIntent = Intent.makeRestartActivityTask(componentName)
context.startActivity(mainIntent)
Runtime.getRuntime().exit(0)
def share_text(text, title='Share', chooser=False, app_package=None, call_playstore=True, error_msg=""):
intent = Intent()
intent.setAction(Intent.ACTION_SEND)
intent.putExtra(Intent.EXTRA_TEXT, String(str(text)))
intent.setType("text/plain")
if app_package:
app_package = packages[app_package] if app_package in packages else None
try:
intent.setPackage(String(app_package))
except JavaException:
if call_playstore:
webbrowser.open(f"http://play.google.com/store/apps/details?id={app_package}")
toast(error_msg) if error_msg else Logger.error("Kvdroid: Specified Application is unavailable")
return
if chooser:
chooser = Intent.createChooser(intent, String(title))
activity.startActivity(chooser)
else:
activity.startActivity(intent)
def share_file(path, title='Share', chooser=True, app_package=None, call_playstore=True, error_msg=""):
path = str(path)
if VERSION.SDK_INT>=24:
StrictMode.disableDeathOnFileUriExposure()
shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.setType("*/*")
imageFile = File(path)
uri = Uri.fromFile(imageFile)
parcelable = cast('android.os.Parcelable', uri)
shareIntent.putExtra(Intent.EXTRA_STREAM, parcelable)
if app_package:
app_package = packages[app_package] if app_package in packages else None
try:
shareIntent.setPackage(String(app_package))
except JavaException:
if call_playstore:
webbrowser.open(f"http://play.google.com/store/apps/details?id={app_package}")
toast(error_msg) if error_msg else Logger.error("Kvdroid: Specified Application is unavailable")
return
if chooser:
chooser = Intent.createChooser(shareIntent, String(title))
activity.startActivity(chooser)
else:
activity.startActivity(shareIntent)
def mime_type(file_path):
return URLConnection.guessContentTypeFromName(file_path)
else:
Logger.error(
"Kvdroid: Kvdroid is only callable for Android"
)