-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
285 lines (250 loc) · 9.43 KB
/
main.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"
--获取主题资源
local array = 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=...
local intent=activity.getIntent()
--初始工程路径与文件路径
initPrjPath=intent.getStringExtra(EXTRA_FILE_PATH) or initPrjPath
initFilePath=intent.getStringExtra(EXTRA_FILE_PATH) or initFilePath
--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=initFilePath and FileUtil.getParent(initFilePath) or initPrjPath
initShortPath=initFilePath and shortPath(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)
--修复点击后自动滚动问题
if Build.VERSION.SDK_INT>=25 then
messageView.setRevealOnFocusHint(false)
end
messageView.setText(Html.fromHtml(decodeTemplateCode(readFile(rel2AbsPath(i18n("messagePath"),luajava.luadir)))))
function onCreateOptionsMenu(menu)
menu.add(0,1,0,i18n("sourceRepository"))
menu.add(0,2,0,i18n("openSourceLicenses"))
end
function onOptionsItemSelected(item)
local id=item.getItemId()
local title=item.title
if id==android.R.id.home then
activity.finish()
elseif id==1 then
local intent = Intent("android.intent.action.VIEW",Uri.parse(URL_REPOSITORY))
activity.startActivity(intent)
elseif id==2 then
local dialog=AlertDialog.Builder(this)
.setTitle(i18n("openSourceLicenses"))
.setMessage(Html.fromHtml(readFile(luajava.luadir.."/openSourceLicenses.html")))
.setPositiveButton(android.R.string.ok,nil)
.show()
local messageView=dialog.findViewById(android.R.id.message)
messageView.setTextIsSelectable(true)
messageView.setMovementMethod(RTEditorMovementMethod.getInstance())
end
end
---获取工程路径
function getPrjPath()
return prjPathEdit.text
end
---获取ALY路径
function getAlyPath()
return rel2AbsPath(alyPathEdit.text,getPrjPath())
end
function updateProjectPath(newPrjPath)
local alyPath=getAlyPath()
prjPathEdit.text=newPrjPath
updateAlpPath(alyPath)
end
function updateAlpPath(newAlpPath)
alyPathEdit.text=shortPath(newAlpPath,getPrjPath())
end
function compileFunc(importText,envPath,prjPath,alyPath)
return pcall(function()
require "import"
--import "android.net.Uri"
--import "android.os.Environment"
--import "java.io.FileInputStream"
--import "java.io.FileOutputStream"
for line in (importText.."\n"):gmatch("(.-)\n") do
if line~="" then
_G["import"](line)
end
end
import "compilelayout"
return compilelayout(alyPath,envPath)
end)
end
function compileCallback(success,content)
local dialog
local builder=AlertDialog.Builder(this)
if success then
local editor=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()
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)
intent.putExtra(Intent.EXTRA_TITLE, "LayoutHelper.lua")
local prjPath=prjPathEdit.text
local parentFilePath=FileUtil.getParent(getAlyPath())
if parentFilePath and parentFilePath~="" and parentFilePath:sub(1,#sdcardPath)==sdcardPath then
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, FileUriUtil.path2DocumentUri(parentFilePath,false))
elseif prjPath and prjPath~="" and prjPath:sub(1,#sdcardPath)==sdcardPath then
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, FileUriUtil.path2DocumentUri(prjPath,false))
end
activity.startActivityForResult(intent,EXPORT_HELPER)
end
compileButton.onClick=function()
local err=false
--compilelayout(getAlyPath(),pathEdit.text)
if not File(getAlyPath()).isFile() then
err=true
alyPathEdit.setError(i18n("error.fileNotFound"))
end
if err then
return
end
activity.newTask(compileFunc,compileCallback)
.execute({importEdit.text, pathEdit.text, getPrjPath(), getAlyPath()})
end
alyPathSelectButton.onClick=function()
local intent = Intent(Intent.ACTION_GET_CONTENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.setType("application/octet-stream")
intent.putExtra(Intent.EXTRA_MIME_TYPES,TYPES_LUA)
local parentFilePath=FileUtil.getParent(getAlyPath())
if parentFilePath and parentFilePath~="" and parentFilePath:sub(1,#sdcardPath)==sdcardPath then
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()
local intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
local previousPrjPath=prjPathEdit.text
if previousPrjPath and previousPrjPath~="" and previousPrjPath:sub(1,#sdcardPath)==sdcardPath then
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]]
function onActivityResult(requestCode,resultCode,data)
if resultCode == Activity.RESULT_OK then
local uri=data.getData()
if requestCode==EXPORT_HELPER and uri then
local pfd=activity.getContentResolver().openFileDescriptor(uri, "w")
local fileOutputStream=FileOutputStream(pfd.getFileDescriptor())
local fileInputStream=FileInputStream(activity.getLuaDir().."/LayoutHelper.lua")
xpcall(LuaUtil.copyFile,function(errMsg)
print("导出失败",errMsg)
end,fileInputStream,fileOutputStream)
fileOutputStream.close()
fileInputStream.close()
elseif requestCode==PICK_PRJ and uri then
local path=FileUriUtil.getPath(activity, uri)
if path and File(path).isDirectory() and File(path).canRead() then
updateProjectPath(path)
else
toast("路径获取失败,请选择本地文件夹")
end
elseif requestCode==PICK_ALY and uri then
local path=FileUriUtil.getPath(activity, uri)
local prjPath=prjPathEdit.text
if path and File(path).isFile() and File(path).canRead() then
alyPathEdit.text=prjPath and shortPath(path, prjPath) or path
else
toast("路径获取失败,请选择本地文件")
end
elseif requestCode==SELECT_OUTPUT and uri then
local path=FileUriUtil.getPath(activity, uri)
if path and File(path).isFile() then
alyPathEdit.text=prjPath and shortPath(path, prjPath) or path
else
toast("路径获取失败,请选择本地文件")
end
end
end
end