Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
Latest commit
285 lines (250 loc) · 9.43 KB
/
Copy pathmain.lua
File metadata and controls
285 lines (250 loc) · 9.43 KB
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
require"import"
import"android.app.*"
import"android.os.*"
import"android.widget.*"
import"android.view.*"
import"android.provider.DocumentsContract"
import"java.io.File"
import"android.net.Uri"
import"android.content.Intent"
import"java.io.FileInputStream"
import"java.io.FileOutputStream"
import"android.content.Context"
import"android.text.Html"
import"java.util.Locale"
import"com.onegravity.rteditor.RTEditorMovementMethod"
import"utils.DialogHelper"
import"utils.FileUtil"
import"utils.FileUriUtil"
import"themeutil"
import"helper"
import"compilelayout"
import"init"
import"LayoutHelper"
import"layoutTemplate"
import'lua.i18n'
EXPORT_HELPER=1
PICK_ALY=2
SELECT_OUTPUT=3
PICK_PRJ=3
TYPES_LUA=String({"application/octet-stream","application/lua","text/plain"})
EXTRA_PRJ_PATH="prjPath"
EXTRA_FILE_PATH="filePath"
URL_REPOSITORY="https://gitee.com/AideLua/AndroLuaLayoutCompiler"
--禁用华为主题
--androidhwext=nil
--themeutil.isEmuiSystem=false
themeutil.applyTheme()
isAideLua=apptype=="aidelua"
--获取主题资源
localarray=activity.obtainStyledAttributes({
android.R.attr.colorAccent,
android.R.attr.textColorPrimary,
android.R.attr.textColorSecondary,
})
colorAccent=array.getColorStateList(0)
textColorPrimary=array.getColorStateList(1)
textColorSecondary=array.getColorStateList(2)
initPrjPath,initFilePath=...
localintent=activity.getIntent()
--初始工程路径与文件路径
initPrjPath=intent.getStringExtra(EXTRA_FILE_PATH) orinitPrjPath
initFilePath=intent.getStringExtra(EXTRA_FILE_PATH) orinitFilePath
--initPrjPath=initPrjPath or "/storage/emulated/0/AppProjects/AideLuaPro"
--initFilePath=initFilePath or "/storage/emulated/0/AppProjects/AideLuaPro/app/src/main/assets_bin/layouts/buildingLayout.aly"
parentFilePath=initFilePathandFileUtil.getParent(initFilePath) orinitPrjPath
initShortPath=initFilePathandshortPath(initFilePath,initPrjPath)
--initOutputPath=initShortPath and initShortPath:gsub("%.aly$",".lua")
initEnvironmentPath=buildEnvPath(parentFilePath)
i18n.loadFile(luajava.luadir..'/i18n/en.lua')
i18n.loadFile(luajava.luadir..'/i18n/zh.lua')
i18n.setLocale(Locale.getDefault().getLanguage())
activity.setTitle(i18n("appName"))
actionBar.setDisplayHomeAsUpEnabled(true)
actionBar.setSubtitle(string.format("v%s (%s)",appver,appcode))
activity.setContentView(loadlayout("layout"))
activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
--修复点击后自动滚动问题
ifBuild.VERSION.SDK_INT>=25then
messageView.setRevealOnFocusHint(false)
end
messageView.setText(Html.fromHtml(decodeTemplateCode(readFile(rel2AbsPath(i18n("messagePath"),luajava.luadir)))))
functiononCreateOptionsMenu(menu)
menu.add(0,1,0,i18n("sourceRepository"))
menu.add(0,2,0,i18n("openSourceLicenses"))
end
functiononOptionsItemSelected(item)
localid=item.getItemId()
localtitle=item.title
ifid==android.R.id.homethen
activity.finish()
elseifid==1then
localintent=Intent("android.intent.action.VIEW",Uri.parse(URL_REPOSITORY))
activity.startActivity(intent)
elseifid==2then
localdialog=AlertDialog.Builder(this)
.setTitle(i18n("openSourceLicenses"))
.setMessage(Html.fromHtml(readFile(luajava.luadir.."/openSourceLicenses.html")))
.setPositiveButton(android.R.string.ok,nil)
.show()
localmessageView=dialog.findViewById(android.R.id.message)
messageView.setTextIsSelectable(true)
messageView.setMovementMethod(RTEditorMovementMethod.getInstance())
end
end
---获取工程路径
functiongetPrjPath()
returnprjPathEdit.text
end
---获取ALY路径
functiongetAlyPath()
returnrel2AbsPath(alyPathEdit.text,getPrjPath())
end
functionupdateProjectPath(newPrjPath)
localalyPath=getAlyPath()
prjPathEdit.text=newPrjPath
updateAlpPath(alyPath)
end
functionupdateAlpPath(newAlpPath)
alyPathEdit.text=shortPath(newAlpPath,getPrjPath())
end
functioncompileFunc(importText,envPath,prjPath,alyPath)
returnpcall(function()
require"import"
--import "android.net.Uri"
--import "android.os.Environment"
--import "java.io.FileInputStream"
--import "java.io.FileOutputStream"
forlinein (importText.."\n"):gmatch("(.-)\n") do
ifline~="" then
_G["import"](line)
end
end
import"compilelayout"
returncompilelayout(alyPath,envPath)
end)
end
functioncompileCallback(success,content)
localdialog
localbuilder=AlertDialog.Builder(this)
ifsuccessthen
localeditor=LuaEditor(activity)
editor.text=content
builder.setTitle("编译成功")
.setView(editor)
.setPositiveButton(android.R.string.ok,nil)
dialog=builder.show()
editor.format()
editor.setScrollIndicators(View.SCROLL_INDICATOR_TOP | View.SCROLL_INDICATOR_BOTTOM,
View.SCROLL_INDICATOR_TOP | View.SCROLL_INDICATOR_BOTTOM)
else
builder.setTitle("编译错误")
.setMessage(content)
.setPositiveButton(android.R.string.ok,nil)
dialog=builder.show()
end
DialogHelper.setMessageIsSelectable(dialog,true)
end
exportHelperButton.onClick=function()
localintent=Intent(Intent.ACTION_CREATE_DOCUMENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.setType("application/octet-stream")
intent.putExtra(Intent.EXTRA_MIME_TYPES,TYPES_LUA)
intent.putExtra(Intent.EXTRA_TITLE, "LayoutHelper.lua")
localprjPath=prjPathEdit.text
localparentFilePath=FileUtil.getParent(getAlyPath())
ifparentFilePathandparentFilePath~="" andparentFilePath:sub(1,#sdcardPath)==sdcardPaththen
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, FileUriUtil.path2DocumentUri(parentFilePath,false))
elseifprjPathandprjPath~="" andprjPath:sub(1,#sdcardPath)==sdcardPaththen
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, FileUriUtil.path2DocumentUri(prjPath,false))
end
activity.startActivityForResult(intent,EXPORT_HELPER)
end
compileButton.onClick=function()
localerr=false
--compilelayout(getAlyPath(),pathEdit.text)
ifnotFile(getAlyPath()).isFile() then
err=true
alyPathEdit.setError(i18n("error.fileNotFound"))
end
iferrthen
return
end
activity.newTask(compileFunc,compileCallback)
.execute({importEdit.text, pathEdit.text, getPrjPath(), getAlyPath()})
end
alyPathSelectButton.onClick=function()
localintent=Intent(Intent.ACTION_GET_CONTENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.setType("application/octet-stream")
intent.putExtra(Intent.EXTRA_MIME_TYPES,TYPES_LUA)
localparentFilePath=FileUtil.getParent(getAlyPath())
ifparentFilePathandparentFilePath~="" andparentFilePath:sub(1,#sdcardPath)==sdcardPaththen
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, FileUriUtil.path2DocumentUri(parentFilePath,false))
--[[
elseif prjPath and String(prjPath).startsWith(sdcardPath) then--工程目录在SD卡目录下
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, path2DocumentUri(isAideLua and prjPath.."/app/src/main/assets_bin" or prjPath))]]
end
activity.startActivityForResult(intent,PICK_ALY)
end
prjPathSelectButton.onClick=function()
localintent=Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
localpreviousPrjPath=prjPathEdit.text
ifpreviousPrjPathandpreviousPrjPath~="" andpreviousPrjPath:sub(1,#sdcardPath)==sdcardPaththen
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, FileUriUtil.path2DocumentUri(previousPrjPath,false))
end
activity.startActivityForResult(intent,PICK_PRJ)
end
generatePathButton.onClick=function()
pathEdit.text=buildEnvPath(FileUtil.getParent(getAlyPath())) or""
end
--[[
outputPathSelectButton.onClick=function()
local intent = Intent(Intent.ACTION_CREATE_DOCUMENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.setType("application/octet-stream")
intent.putExtra(Intent.EXTRA_MIME_TYPES,TYPES_LUA)
local name=getFileNameWithoutExtensionName(alyPathEdit.text)
if name=="" then
name="layout"
end
intent.putExtra(Intent.EXTRA_TITLE, name..".lua");
activity.startActivityForResult(intent,SELECT_OUTPUT)
end]]
functiononActivityResult(requestCode,resultCode,data)
ifresultCode==Activity.RESULT_OKthen
localuri=data.getData()
ifrequestCode==EXPORT_HELPERandurithen
localpfd=activity.getContentResolver().openFileDescriptor(uri, "w")
localfileOutputStream=FileOutputStream(pfd.getFileDescriptor())
localfileInputStream=FileInputStream(activity.getLuaDir().."/LayoutHelper.lua")
xpcall(LuaUtil.copyFile,function(errMsg)
print("导出失败",errMsg)
end,fileInputStream,fileOutputStream)
fileOutputStream.close()
fileInputStream.close()
elseifrequestCode==PICK_PRJandurithen
localpath=FileUriUtil.getPath(activity, uri)
ifpathandFile(path).isDirectory() andFile(path).canRead() then
updateProjectPath(path)
else
toast("路径获取失败,请选择本地文件夹")
end
elseifrequestCode==PICK_ALYandurithen
localpath=FileUriUtil.getPath(activity, uri)
localprjPath=prjPathEdit.text
ifpathandFile(path).isFile() andFile(path).canRead() then
alyPathEdit.text=prjPathandshortPath(path, prjPath) orpath
else
toast("路径获取失败,请选择本地文件")
end
elseifrequestCode==SELECT_OUTPUTandurithen
localpath=FileUriUtil.getPath(activity, uri)
ifpathandFile(path).isFile() then
alyPathEdit.text=prjPathandshortPath(path, prjPath) orpath
else
toast("路径获取失败,请选择本地文件")
end
end
end
end