forked from badabing2005/PixelFlasher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagisk_downloads.py
344 lines (301 loc) · 15.1 KB
/
magisk_downloads.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/usr/bin/env python
import webbrowser
from urllib.parse import urlparse
import clipboard
import darkdetect
import markdown
import wx
import wx.html
import wx.lib.mixins.listctrl as listmix
import wx.lib.wxpTag
import images as images
from runtime import *
# ============================================================================
# Class HtmlWindow
# ============================================================================
class HtmlWindow(wx.html.HtmlWindow):
def OnLinkClicked(self, link):
webbrowser.open(link.GetHref())
# ============================================================================
# Class ListCtrl
# ============================================================================
class ListCtrl(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
def __init__(self, parent, ID, pos=wx.DefaultPosition, size=wx.DefaultSize, style=0):
wx.ListCtrl.__init__(self, parent, ID, pos, size, style)
listmix.ListCtrlAutoWidthMixin.__init__(self)
# ============================================================================
# Class MagiskDownloads
# ============================================================================
class MagiskDownloads(wx.Dialog):
def __init__(self, *args, **kwargs):
wx.Dialog.__init__(self, *args, **kwargs)
self.SetTitle("Download and Install Magisk")
self.url = None
self.channel = None
self.version = None
self.versionCode = None
self.filename = None
self.release_notes = None
self.package = None
vSizer = wx.BoxSizer(wx.VERTICAL)
message_sizer = wx.BoxSizer(wx.HORIZONTAL)
message_sizer.Add((0, 0), 1, wx.EXPAND, 5)
self.message_label = wx.StaticText(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0)
self.message_label.Wrap(-1)
self.message_label.Label = "Select Magisk version to install."
if sys.platform == "win32":
self.message_label.SetFont(wx.Font(12, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD, False, "Arial"))
message_sizer.Add(self.message_label, 0, wx.ALL, 20)
message_sizer.Add((0, 0), 1, wx.EXPAND, 5)
vSizer.Add(message_sizer, 0, wx.EXPAND, 5)
list_sizer = wx.BoxSizer(wx.HORIZONTAL)
# list control
if self.CharHeight > 20:
self.il = wx.ImageList(24, 24)
self.idx1 = self.il.Add(images.official_24.GetBitmap())
else:
self.il = wx.ImageList(16, 16)
self.idx1 = self.il.Add(images.official_16.GetBitmap())
self.list = ListCtrl(self, -1, size=(-1, self.CharHeight * 15), style = wx.LC_REPORT)
self.list.SetImageList(self.il, wx.IMAGE_LIST_SMALL)
device = get_phone()
apks = device.magisk_apks
self.list.InsertColumn(0, 'Channel', width = -1)
self.list.InsertColumn(1, 'Version', wx.LIST_FORMAT_LEFT, -1)
self.list.InsertColumn(2, 'VersionCode', wx.LIST_FORMAT_LEFT, -1)
self.list.InsertColumn(3, 'URL', wx.LIST_FORMAT_LEFT, -1)
self.list.InsertColumn(4, 'Package', wx.LIST_FORMAT_LEFT, -1)
if sys.platform == "win32":
self.list.SetHeaderAttr(wx.ItemAttr(wx.Colour('BLUE'),wx.Colour('DARK GREY'), wx.Font(wx.FontInfo(10).Bold())))
i = 0
for apk in apks:
if apk.type:
index = self.list.InsertItem(i, apk.type)
if apk.type in ('stable', 'beta', 'canary', 'debug'):
self.list.SetItemColumnImage(i, 0, 0)
else:
self.list.SetItemColumnImage(i, 0, -1)
if apk.version:
self.list.SetItem(index, 1, apk.version)
if apk.versionCode:
self.list.SetItem(index, 2, apk.versionCode)
if apk.link:
self.list.SetItem(index, 3, apk.link)
if apk.package:
self.list.SetItem(index, 4, apk.package)
i += 1
self.list.SetColumnWidth(0, -2)
grow_column(self.list, 0, 20)
self.list.SetColumnWidth(1, -2)
grow_column(self.list, 1, 20)
self.list.SetColumnWidth(2, -2)
grow_column(self.list, 2, 20)
self.list.SetColumnWidth(3, -2)
grow_column(self.list, 3, 20)
self.list.SetColumnWidth(4, -1)
grow_column(self.list, 4, 20)
list_sizer.Add(self.list, 1, wx.ALL|wx.EXPAND, 10)
vSizer.Add(list_sizer , 0, wx.EXPAND, 5)
# Release Notes
self.html = HtmlWindow(self, wx.ID_ANY, size=(420, -1))
if darkdetect.isDark():
black_html = "<!DOCTYPE html>\n<html><body style=\"background-color:#1e1e1e;\"></body></html>"
self.html.SetPage(black_html)
if "gtk2" in wx.PlatformInfo or "gtk3" in wx.PlatformInfo:
self.html.SetStandardFonts()
vSizer.Add(self.html , 1, wx.EXPAND | wx.ALL, 10)
buttons_sizer = wx.BoxSizer(wx.HORIZONTAL)
buttons_sizer.Add((0, 0), 1, wx.EXPAND, 5)
self.install_button = wx.Button(self, wx.ID_ANY, u"Install", wx.DefaultPosition, wx.DefaultSize, 0)
self.install_button.SetToolTip(u"WARNING! Do not install magisk if you already have a hidden (stub) Magisk installed.\nFirst unhide Magisk before attempting an install.")
self.install_button.Enable(False)
buttons_sizer.Add(self.install_button, 0, wx.ALL, 20)
self.cancel_button = wx.Button(self, wx.ID_ANY, u"Cancel", wx.DefaultPosition, wx.DefaultSize, 0)
buttons_sizer.Add(self.cancel_button, 0, wx.ALL, 20)
buttons_sizer.Add((0, 0), 1, wx.EXPAND, 5)
vSizer.Add(buttons_sizer, 0, wx.EXPAND, 5)
self.SetSizer(vSizer)
self.Layout()
self.Centre(wx.BOTH)
# Connect Events
self.install_button.Bind(wx.EVT_BUTTON, self._onOk)
self.cancel_button.Bind(wx.EVT_BUTTON, self._onCancel)
self.list.Bind(wx.EVT_LEFT_DOWN, self._on_apk_selected)
self.list.Bind(wx.EVT_RIGHT_DOWN, self._onRightDown)
# for wxMSW
self.list.Bind(wx.EVT_COMMAND_RIGHT_CLICK, self._onRightClick)
# for wxGTK
self.list.Bind(wx.EVT_RIGHT_UP, self._onRightClick)
self.Bind(wx.EVT_LIST_ITEM_SELECTED, self._onItemSelected, self.list)
# Autosize the dialog
self.list.PostSizeEventToParent()
self.SetSizerAndFit(vSizer)
a = self.list.GetViewRect()
self.SetSize(vSizer.MinSize.Width + 80, vSizer.MinSize.Height + 420)
print("\nOpening Magisk Downloader/Installer ...")
puml(f":Open Magisk Downloader/Installer;\n", True)
# -----------------------------------------------
# _onItemSelected
# -----------------------------------------------
def _onItemSelected(self, event):
self.currentItem = event.Index
print(f"Magisk {self.list.GetItemText(self.currentItem)} is selected.")
puml(f":Select Magisk {self.list.GetItemText(self.currentItem)};\n")
event.Skip()
# -----------------------------------------------
# _onRightDown
# -----------------------------------------------
def _onRightDown(self, event):
x = event.GetX()
y = event.GetY()
# print("x, y = %s\n" % str((x, y)))
item, flags = self.list.HitTest((x, y))
if item != wx.NOT_FOUND and flags & wx.LIST_HITTEST_ONITEM:
self.list.Select(item)
event.Skip()
# -----------------------------------------------
# _onRightClick
# -----------------------------------------------
def _onRightClick(self, event):
# print("OnRightClick %s\n" % self.list.GetItemText(self.currentItem))
# only do this part the first time so the events are only bound once
if not hasattr(self, "popupDisable"):
self.popupCopyURL = wx.NewIdRef()
self.popupCopyPackageId = wx.NewIdRef()
self.Bind(wx.EVT_MENU, self._OnCopyURL, id=self.popupCopyURL)
self.Bind(wx.EVT_MENU, self._OnCopyPackageId, id=self.popupCopyPackageId)
# build the menu
menu = wx.Menu()
menu.Append(self.popupCopyURL, "Copy URL to Clipboard")
menu.Append(self.popupCopyPackageId, "Copy Package ID to Clipboard")
# Popup the menu. If an item is selected then its handler
# will be called before PopupMenu returns.
self.PopupMenu(menu)
menu.Destroy()
# -----------------------------------------------
# _OnCopyPackageId
# -----------------------------------------------
def _OnCopyPackageId(self, event):
item = self.list.GetItem(self.currentItem, 4)
clipboard.copy(item.Text)
# -----------------------------------------------
# _OnCopyURL
# -----------------------------------------------
def _OnCopyURL(self, event):
item = self.list.GetItem(self.currentItem, 3)
clipboard.copy(item.Text)
# -----------------------------------------------
# __del__
# -----------------------------------------------
def __del__(self):
pass
# -----------------------------------------------
# _onCancel
# -----------------------------------------------
def _onCancel(self, e):
puml(f":Cancelled Magisk Downloader/Installer;\n", True)
self.EndModal(wx.ID_CANCEL)
# -----------------------------------------------
# _on_apk_selected
# -----------------------------------------------
def _on_apk_selected(self, e):
x,y = e.GetPosition()
row,flags = self.list.HitTest((x,y))
for i in range (0, self.list.ItemCount):
# deselect all items
self.list.Select(i, 0)
item = self.list.GetItem(i)
# reset colors
if sys.platform == "win32":
item.SetTextColour(wx.BLACK)
elif darkdetect.isDark():
item.SetTextColour(wx.WHITE)
self.list.SetItem(item)
if row != -1:
self.list.Select(row)
item = self.list.GetItem(row)
if sys.platform == "win32":
item.SetTextColour(wx.BLUE)
self.list.SetItem(item)
self.channel = self.list.GetItemText(row, col=0)
self.version = self.list.GetItemText(row, col=1)
self.versionCode = self.list.GetItemText(row, col=2)
self.url = self.list.GetItemText(row, col=3)
self.filename = os.path.basename(urlparse(self.url).path)
self.package = self.list.GetItemText(row, col=4)
device = get_phone()
apks = device.magisk_apks
release_notes = apks[row].release_notes
# convert markdown to html
self.release_notes = markdown.markdown(release_notes)
self.html.SetPage(self.release_notes)
self.install_button.Enable(True)
else:
self.install_button.Enable(False)
# -----------------------------------------------
# _onOk
# -----------------------------------------------
def _onOk(self, e):
proceed = True
print(f"{datetime.now():%Y-%m-%d %H:%M:%S} User Pressed Ok.")
filename = f"magisk_{self.version}_{self.versionCode}.apk"
device = get_phone()
if 'Namelesswonder' in self.url and not device.has_init_boot:
print(f"WARNING: The selected Magisk is not supported for your device: {device.hardware}")
print(" Only Pixel 7 (panther) and Pixel 7 Pro (cheetah) and Pixel 7a (lynx) and Pixel Tablet (tangorpro) are currently supported.")
print(" See details at: https://forum.xda-developers.com/t/magisk-magisk-zygote64_32-enabling-32-bit-support-for-apps.4521029/")
title = "Device Not Supported"
message = f"ERROR: Your phone model is: {device.hardware}\n\n"
message += "The selected Magisk is not supported for your device\n"
message += "Only Pixel 7 (panther) and Pixel 7 Pro (cheetah) and Pixel 7a (lynx) and Pixel Tablet (tangorpro) are currently supported.\n\n"
message += "Unless you know what you are doing, if you choose to continue\n"
message += "you risk running into serious issues, proceed only if you are absolutely\n"
message += "certian that this is what you want, you have been warned.\n\n"
message += "Click OK to accept and continue.\n"
message += "or Hit CANCEL to abort."
print(f"\n*** Dialog ***\n{message}\n______________\n")
# puml(":Dialog;\n")
# puml(f"note right\n{message}\nend note\n")
dlg = wx.MessageDialog(None, message, title, wx.CANCEL | wx.OK | wx.ICON_EXCLAMATION)
result = dlg.ShowModal()
if result == wx.ID_OK:
print("User pressed ok.")
# puml(":User Pressed OK;\n")
else:
print("User pressed cancel.")
print("Aborting ...\n")
# puml("#pink:User Pressed Cancel to abort;\n}\n")
proceed = False
if proceed:
print(f"Downloading Magisk: {self.channel} version: {self.version} versionCode: {self.versionCode} ...")
download_file(self.url, filename)
config_path = get_config_path()
app = os.path.join(config_path, 'tmp', filename)
device.install_apk(app, fastboot_included = True)
# Fresh install of Magisk, reset the package name to default value
set_magisk_package(self.package)
self.Parent.config.magisk = self.package
print('')
self.EndModal(wx.ID_OK)
# ============================================================================
# Function download_file
# ============================================================================
def download_file(url, filename = None):
if url:
print (f"Downloading File: {url}")
try:
response = requests.get(url)
config_path = get_config_path()
if not filename:
filename = os.path.basename(urlparse(url).path)
downloaded_file_path = os.path.join(config_path, 'tmp', filename)
open(downloaded_file_path, "wb").write(response.content)
# check if filename got downloaded
if not os.path.exists(downloaded_file_path):
print(f"\n{datetime.now():%Y-%m-%d %H:%M:%S} ERROR: Failed to download file from {url}\n")
print("Aborting ...\n")
return
except Exception:
print (f"\n{datetime.now():%Y-%m-%d %H:%M:%S} ERROR: Failed to download file from {url}\n")
return 'ERROR'
return downloaded_file_path