From d478e8794130193099d04911fbcb4edcd96cbe38 Mon Sep 17 00:00:00 2001 From: PangBaiWork <15183252672@163.com> Date: Sat, 27 Apr 2024 23:26:08 +0800 Subject: [PATCH] change scripts position --- README-CN.md | 2 +- README.md | 2 +- app/release/output-metadata.json | 20 ---- .../pangbai/weblog/activity/HomeActivity.java | 3 - .../pangbai/weblog/activity/MainActivity.java | 11 +-- .../weblog/preference/PrefManager.java | 14 ++- .../pangbai/weblog/project/PostCreate.java | 4 +- .../com/pangbai/weblog/project/Project.java | 8 +- .../weblog/project/ProjectManager.java | 5 +- .../main/java/com/pangbai/weblog/tool/IO.java | 1 + .../java/com/pangbai/weblog/tool/Init.java | 99 +++++++++++-------- 11 files changed, 85 insertions(+), 84 deletions(-) delete mode 100644 app/release/output-metadata.json diff --git a/README-CN.md b/README-CN.md index 19f1941..56d9aaa 100644 --- a/README-CN.md +++ b/README-CN.md @@ -79,7 +79,7 @@ git clone https://x-access-token:你的令牌@github.com/your-username/your-repo ```shell git config --global user.name "yourname" git config --global user.email "youremail" -ssh-keygen -t rsa -C 你的邮箱 +ssh-keygen -t rsa -C "youremail" ``` 连续回车两次 终端输出完信息后,再执行 diff --git a/README.md b/README.md index 5f20e56..dd156e3 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,7 @@ Open the terminal at the bottom of WeBlog and type this. ```shell git config --global user.name "yourname" git config --global user.email "youremail" -ssh-keygen -t rsa -C your email. +ssh-keygen -t rsa -C "youremail" ``` Enter twice . After the terminal has finished outputting the message, run diff --git a/app/release/output-metadata.json b/app/release/output-metadata.json deleted file mode 100644 index 87ae378..0000000 --- a/app/release/output-metadata.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "version": 3, - "artifactType": { - "type": "APK", - "kind": "Directory" - }, - "applicationId": "com.pangbai.weblog", - "variantName": "release", - "elements": [ - { - "type": "SINGLE", - "filters": [], - "attributes": [], - "versionCode": 3, - "versionName": "1.2", - "outputFile": "app-release.apk" - } - ], - "elementType": "File" -} \ No newline at end of file diff --git a/app/src/main/java/com/pangbai/weblog/activity/HomeActivity.java b/app/src/main/java/com/pangbai/weblog/activity/HomeActivity.java index 101db10..507706c 100644 --- a/app/src/main/java/com/pangbai/weblog/activity/HomeActivity.java +++ b/app/src/main/java/com/pangbai/weblog/activity/HomeActivity.java @@ -81,7 +81,6 @@ public void onClick(View v) { AlertDialog dialog = DialogUtils.showLoadingDialog(this); ThreadUtil.thread(() -> { boolean init = projectManager.createProject(); - if (init) projectManager.createScript(HomeActivity.this); runOnUiThread(() -> { dialog.dismiss(); if (init) { @@ -117,7 +116,6 @@ public void onClick(View v) { return; } selectProject = new Project(file.getName(), file.getAbsolutePath(), type); - new ProjectManager(selectProject).createScript(HomeActivity.this); } dialog.dismiss(); runOnUiThread(() -> { @@ -142,7 +140,6 @@ public void onClick(View v) { return; } selectProject = new Project(file.getName(), file.getAbsolutePath(), type); - new ProjectManager(selectProject).createScript(HomeActivity.this); openProject(selectProject); }); } else if (id == R.id.open_terminal) { diff --git a/app/src/main/java/com/pangbai/weblog/activity/MainActivity.java b/app/src/main/java/com/pangbai/weblog/activity/MainActivity.java index 4bab7df..c58c772 100644 --- a/app/src/main/java/com/pangbai/weblog/activity/MainActivity.java +++ b/app/src/main/java/com/pangbai/weblog/activity/MainActivity.java @@ -25,7 +25,6 @@ import com.google.android.material.bottomsheet.BottomSheetBehavior; import com.google.android.material.navigation.NavigationView; -import com.google.android.material.search.SearchView; import com.google.android.material.snackbar.Snackbar; import com.google.android.material.tabs.TabLayout; @@ -64,9 +63,7 @@ import br.tiagohm.markdownview.MarkdownView; import br.tiagohm.markdownview.css.styles.Github; -import io.github.rosemoe.sora.event.EventReceiver; import io.github.rosemoe.sora.event.PublishSearchResultEvent; -import io.github.rosemoe.sora.event.Unsubscribe; import io.github.rosemoe.sora.text.Content; import io.github.rosemoe.sora.text.ContentIO; import io.github.rosemoe.sora.widget.EditorSearcher; @@ -90,7 +87,7 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); PrefManager.init(getApplicationContext()); project = ProjectManager.getCurrentProject(); - if (PrefManager.isFirstLaunch() || project == null || !new File(project.getProjectPath()).exists()) { + if (PrefManager.isFirstOrReinstall(this) || project == null || !new File(project.getProjectPath()).exists()) { util.startActivity(this, HomeActivity.class, false); finish(); return; @@ -202,14 +199,14 @@ void setLayout() { int id = item.getItemId(); if (id == R.id.menu_run) { - File file = new File(project.scriptPath); + File file = new File(project.getScriptPath()); if (!file.exists()) return false; DialogUtils.showMultiSelectDialog(this, getString(R.string.select_scripts), file.list(), selets -> { binding.progressbar.setIndeterminate(true); Snackbar.make(binding.getRoot(), "Excuting", Snackbar.LENGTH_SHORT).show(); ThreadUtil.thread(() -> { - boolean cmd = cmdExer.executeScripts(selets, project.scriptPath, true) == 0; + boolean cmd = cmdExer.executeScripts(selets, project.getScriptPath(), true) == 0; runOnUiThread(() -> { binding.progressbar.setIndeterminate(false); if (cmd) { @@ -415,7 +412,7 @@ public boolean onNavigationItemSelected(@NonNull MenuItem item) { } else if (id == R.id.project_script) { - new FileListSelect(this, getString(R.string.scripts_list), false, project.scriptPath, file -> { + new FileListSelect(this, getString(R.string.scripts_list), false, project.getScriptPath(), file -> { setCodeText(file); binding.drawerLayout.closeDrawer(GravityCompat.START); }).showChooseDialog(); diff --git a/app/src/main/java/com/pangbai/weblog/preference/PrefManager.java b/app/src/main/java/com/pangbai/weblog/preference/PrefManager.java index 426784d..f5fe1f9 100644 --- a/app/src/main/java/com/pangbai/weblog/preference/PrefManager.java +++ b/app/src/main/java/com/pangbai/weblog/preference/PrefManager.java @@ -14,12 +14,14 @@ public enum Keys { category, current_project, current_file, - first_launch, + first_launch_mark, bl_interface_undo_button_display, bl_artical_title_to_md5 } static SharedPreferences pref; + //1 for first installation , 2 for reinstalltation + public static int isFirstOrReinstall_flag=0; @@ -30,9 +32,13 @@ public static void init(Context context) { } - public static boolean isFirstLaunch() { - if (getBoolen(Keys.first_launch,true)) { - putBoolen(Keys.first_launch,false); + public static boolean isFirstOrReinstall(Context context) { + String mark= context.getApplicationInfo().nativeLibraryDir; + String defaults="defaults"; + String marked=getString(Keys.first_launch_mark,defaults); + if (!mark.equals(marked)) { + putString(Keys.first_launch_mark,mark); + isFirstOrReinstall_flag=marked.equals(defaults)?1:2; return true; } return false; diff --git a/app/src/main/java/com/pangbai/weblog/project/PostCreate.java b/app/src/main/java/com/pangbai/weblog/project/PostCreate.java index 704acb5..a806434 100644 --- a/app/src/main/java/com/pangbai/weblog/project/PostCreate.java +++ b/app/src/main/java/com/pangbai/weblog/project/PostCreate.java @@ -27,11 +27,11 @@ public void setParams(String title, String path, String category, String descrip File file = new File(path); if (file.exists()) return ; File dir = file.getParentFile(); - if (!dir.exists()) dir.mkdirs(); + if (!dir.exists()) dir.mkdirs(); try { file.createNewFile(); writer = new PrintWriter(new FileWriter(file), true); - } catch (Exception e) { + } catch (Exception ignored) { } } diff --git a/app/src/main/java/com/pangbai/weblog/project/Project.java b/app/src/main/java/com/pangbai/weblog/project/Project.java index 14bfb19..0a941e4 100644 --- a/app/src/main/java/com/pangbai/weblog/project/Project.java +++ b/app/src/main/java/com/pangbai/weblog/project/Project.java @@ -4,12 +4,13 @@ import com.pangbai.weblog.execute.HexoExer; import com.pangbai.weblog.execute.HugoExer; import com.pangbai.weblog.project.ProjectManager; +import com.pangbai.weblog.tool.Init; public class Project { String projectPath; String blogName; public ProjectManager.Type blogType; - public String scriptPath; + //public String scriptPath; public BlogCmd blogCmd; public Project( String name,String path, ProjectManager.Type type) { @@ -21,7 +22,7 @@ public Project( String name,String path, ProjectManager.Type type) { } else if (type== ProjectManager.Type.hugo) { blogCmd=new HugoExer(projectPath); } - scriptPath=projectPath+"/.scripts"; + // scriptPath=projectPath+"/.scripts"; } public String getProjectPath() { @@ -39,4 +40,7 @@ public String getBlogName() { public void setBlogName(String blogName) { this.blogName = blogName; } + public String getScriptPath(){ + return Init.scriptsPath+blogType; + } } diff --git a/app/src/main/java/com/pangbai/weblog/project/ProjectManager.java b/app/src/main/java/com/pangbai/weblog/project/ProjectManager.java index c6edc82..3cdabf4 100644 --- a/app/src/main/java/com/pangbai/weblog/project/ProjectManager.java +++ b/app/src/main/java/com/pangbai/weblog/project/ProjectManager.java @@ -26,6 +26,7 @@ public ProjectManager(Project project) { } + public boolean createProject() { boolean init = project.blogCmd.initBlog(project.projectPath); if (!init) return false; @@ -33,11 +34,11 @@ public boolean createProject() { return true; } - public boolean createScript(Context context) { + /* public boolean createScript(Context context) { if (new File(project.scriptPath).exists()) return true; IO.copyAssetsDirToSDCard(context, "scripts/" + project.blogType.name(), project.scriptPath); return true; - } + }*/ public static void saveCurrentProject(Project project) { String[] infor = new String[]{project.blogName, project.getProjectPath(), project.blogType.name()}; diff --git a/app/src/main/java/com/pangbai/weblog/tool/IO.java b/app/src/main/java/com/pangbai/weblog/tool/IO.java index 7e87cce..8c8d5d0 100644 --- a/app/src/main/java/com/pangbai/weblog/tool/IO.java +++ b/app/src/main/java/com/pangbai/weblog/tool/IO.java @@ -40,6 +40,7 @@ public static void copyAssetsDirToSDCard(Context context, String assetsDirName, fos.write(mByte, 0, bt); // dealtByte+=bt; } + fos.getFD().sync(); fos.flush(); inputStream.close(); fos.close(); diff --git a/app/src/main/java/com/pangbai/weblog/tool/Init.java b/app/src/main/java/com/pangbai/weblog/tool/Init.java index 2800157..9ff4411 100755 --- a/app/src/main/java/com/pangbai/weblog/tool/Init.java +++ b/app/src/main/java/com/pangbai/weblog/tool/Init.java @@ -3,8 +3,6 @@ import android.Manifest; import android.annotation.SuppressLint; import android.app.Activity; - -import android.app.Dialog; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; @@ -15,19 +13,19 @@ import android.util.Log; import android.widget.Toast; - import androidx.appcompat.app.AlertDialog; import androidx.core.app.ActivityCompat; import com.pangbai.weblog.R; import com.pangbai.weblog.execute.cmdExer; +import com.pangbai.weblog.preference.PrefManager; +import com.pangbai.weblog.project.ProjectManager; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import br.tiagohm.markdownview.Utils; import io.github.rosemoe.sora.langs.textmate.registry.FileProviderRegistry; import io.github.rosemoe.sora.langs.textmate.registry.provider.AssetsFileResolver; @@ -40,14 +38,15 @@ public class Init { // public static String shellPath; public static String libDir, binDir, nodeDir, weblogDir; - public static String busyboxPath, shPath; + public static String busyboxPath, shPath,scriptsPath; public static String[] envp; final public static String sdcardPath = "/storage/emulated/0/"; final public static String linker = "/system/bin/linker64"; public static String keyPath; - final public static boolean android10=Build.VERSION.SDK_INT>=29; + final public static boolean android10 = Build.VERSION.SDK_INT >= 29; boolean result; + AlertDialog dialog; public static String systemShell = "/system/bin/sh"; @@ -65,6 +64,7 @@ public Init(Activity ct) { nodeDir = binDir + "/node"; weblogDir = filesDirPath + "/weblog"; keyPath = filesDirPath + "/weblog/keys"; + scriptsPath = filesDirPath + "/script/"; String[] envp = { //"PATH=" + "/system/bin" "PATH=" + Init.binDir + ":/product/bin:/apex/com.android.runtime/bin:/apex/com.android.art/bin:/system_ext/bin:/system/bin:/system/xbin:/vendor/bin", @@ -76,52 +76,53 @@ public Init(Activity ct) { "PS1=\\[\\e[1\\;31m\\])➜ \\[\\e[1;36m\\]\\W\\[\\e[m\\] ", "TERM=xterm-256color", "LANG=en_US.UTF-8", - android10?"ANDROID10=1":"ANDROID10=0", + android10 ? "ANDROID10=1" : "ANDROID10=0", "ANDROID_DATA=/data", "ANDROID_ROOT=/system" }; Init.envp = envp; - if (!new File(binDir).exists()) { - dialog = DialogUtils.showLoadingDialog(ct, ct.getString(R.string.load_resources)); - - new Thread() { - @Override - public void run() { - try { - result = installEnv(ct); - } catch (IOException | InterruptedException e) { - result = false; - // throw new RuntimeException(e); - } + if (PrefManager.isFirstOrReinstall_flag==1) { + //First installation + dialog = DialogUtils.showLoadingDialog(ct, ct.getString(R.string.load_resources)); + ThreadUtil.thread(() -> { + try { + result = installEnv(ct); + } catch (IOException | InterruptedException e) { + Log.e("InstallEnv Failed",e.getMessage()); + result = false; + } - new File(filesDirPath + "/home").mkdirs(); - dialog.dismiss(); - util.runOnUiThread(() -> { - if (result) - DialogUtils.showConfirmationDialog(ct, "Environment installation successful", ct.getString(R.string.ask_install_hexo), - () -> { - dialog = DialogUtils.showLoadingDialog(ct, "Installing hexo"); - installHexo(ct); - }, () -> { - dialog.dismiss(); - checkPermission(ct); - }); - else - checkPermission(ct); + initScript(ct); + dialog.dismiss(); + util.runOnUiThread(() -> { + if (result) + DialogUtils.showConfirmationDialog(ct, "Environment installation successful", ct.getString(R.string.ask_install_hexo), + () -> { + dialog = DialogUtils.showLoadingDialog(ct, "Installing hexo "); + installHexo(ct); + }, () -> { + dialog.dismiss(); + checkPermission(ct); + }); + else + checkPermission(ct); - }); + }); - } - }.start(); + } + ); + } else if (PrefManager.isFirstOrReinstall_flag==2) { + //reinstalltion in case of test and update + initScript(ct); } @@ -129,7 +130,7 @@ public void run() { @SuppressLint("SuspiciousIndentation") boolean installEnv(Context context) throws IOException, InterruptedException { - // String name = "busybox"; + // String name = "busybox"; IO.copyAssetsDirToSDCard(context, "busybox", binDir); IO.copyAssetsDirToSDCard(context, "libexec.so", libDir); new File(busyboxPath).setExecutable(true); @@ -157,11 +158,27 @@ boolean installEnv(Context context) throws IOException, InterruptedException { inputStream.close(); outputStream.close(); // int exit = cmdExer.process.waitFor(); - return cmdExer.process.waitFor()==0; + return cmdExer.process.waitFor() == 0; } + void initScript(Context context) { + + boolean result; + new File(filesDirPath + "/home").mkdirs(); + + for (String type : ProjectManager.getTypeArray()) { + String scripts = scriptsPath+ type; + result = new File(scripts).mkdirs(); + if (!result) continue; + IO.copyAssetsDirToSDCard(context, "scripts/" + type, scripts); + } + + + } + + void installHexo(Activity ct) { ThreadUtil.thread(() -> { cmdExer.execute("npm config set registry http://mirrors.cloud.tencent.com/npm/", false); @@ -176,8 +193,6 @@ void installHexo(Activity ct) { } - - private static final int REQUEST_EXTERNAL_STORAGE = 1; private static final String[] PERMISSIONS_STORAGE = { Manifest.permission.READ_EXTERNAL_STORAGE, @@ -201,7 +216,7 @@ public void checkPermission(Activity ct) { } else { // havePermission = true; - Log.i("swyLog", "Android 11以上,当前已有权限"); + Log.i("Permission", "Android 11以上,当前已有权限"); } } else { if (ActivityCompat.checkSelfPermission(ct, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { @@ -216,7 +231,7 @@ public void checkPermission(Activity ct) { }, null); } else { // havePermission = true; - Log.i("swyLog", "Android 6.0以上,11以下,当前已有权限"); + Log.i("Permission", "Android 6.0以上,11以下,当前已有权限"); } } }