From 4692ed4b4a1cc97717a11007fac2f94de39e8824 Mon Sep 17 00:00:00 2001 From: dvdandroid Date: Wed, 17 Aug 2016 13:00:55 +0200 Subject: [PATCH] Load modules in a listview --- app/src/main/AndroidManifest.xml | 10 +- .../com/topjohnwu/magisk/MainActivity.java | 3 + .../java/com/topjohnwu/magisk/Module.java | 83 ++++++++++ .../com/topjohnwu/magisk/ModulesActivity.java | 132 ++++++++++++++++ app/src/main/res/layout/activity_main.xml | 148 +++++++++--------- app/src/main/res/layout/activity_modules.xml | 14 ++ app/src/main/res/layout/row.xml | 32 ++++ app/src/main/res/values-w820dp/dimens.xml | 6 - app/src/main/res/values/dimens.xml | 5 - app/src/main/res/values/strings.xml | 1 + 10 files changed, 347 insertions(+), 87 deletions(-) create mode 100644 app/src/main/java/com/topjohnwu/magisk/Module.java create mode 100644 app/src/main/java/com/topjohnwu/magisk/ModulesActivity.java create mode 100644 app/src/main/res/layout/activity_modules.xml create mode 100644 app/src/main/res/layout/row.xml delete mode 100644 app/src/main/res/values-w820dp/dimens.xml delete mode 100644 app/src/main/res/values/dimens.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 326ddfc55..e95f185f7 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,6 +1,6 @@ - + - + - + + + \ No newline at end of file diff --git a/app/src/main/java/com/topjohnwu/magisk/MainActivity.java b/app/src/main/java/com/topjohnwu/magisk/MainActivity.java index f20c44580..ab32d65ab 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MainActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/MainActivity.java @@ -1,6 +1,7 @@ package com.topjohnwu.magisk; import android.app.Activity; +import android.content.Intent; import android.graphics.Color; import android.os.AsyncTask; import android.os.Bundle; @@ -53,6 +54,8 @@ public class MainActivity extends Activity { new SU().execute("setenforce 0"); } }); + + findViewById(R.id.modules).setOnClickListener(view -> startActivity(new Intent(this, ModulesActivity.class))); } private String execute(String command) { diff --git a/app/src/main/java/com/topjohnwu/magisk/Module.java b/app/src/main/java/com/topjohnwu/magisk/Module.java new file mode 100644 index 000000000..26c3e4346 --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/Module.java @@ -0,0 +1,83 @@ +package com.topjohnwu.magisk; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +public class Module { + + private final boolean isValid; + private final boolean isCache; + private File mPropFile; + + private String mName; + private String mVersion; + private int mVersionCode; + private String mDescription; + + public Module(File file) { + this.isCache = file.getPath().contains("cache"); + this.isValid = new File(file + "/module.prop").exists(); + + if (isValid) { + mPropFile = new File(file + "/module.prop"); + } + } + + public boolean isValid() { + return isValid && mPropFile != null; + } + + public boolean isCache() { + return isCache; + } + + public String getName() { + return mName; + } + + public int getVersionCode() { + return mVersionCode; + } + + public String getVersion() { + return mVersion; + } + + public String getDescription() { + return mDescription; + } + + public void parse() throws Exception { + BufferedReader reader = new BufferedReader(new FileReader(mPropFile)); + String line; + while ((line = reader.readLine()) != null) { + String[] parts = line.split("=", 2); + if (parts.length != 2) { + continue; + } + + String key = parts[0].trim(); + if (key.charAt(0) == '#') { + continue; + } + + String value = parts[1].trim(); + switch (key) { + case "name": + mName = value; + break; + case "version": + mVersion = value; + break; + case "versionCode": + mVersionCode = Integer.parseInt(value); + break; + case "description": + mDescription = value; + break; + } + } + reader.close(); + } +} \ No newline at end of file diff --git a/app/src/main/java/com/topjohnwu/magisk/ModulesActivity.java b/app/src/main/java/com/topjohnwu/magisk/ModulesActivity.java new file mode 100644 index 000000000..73cac7a76 --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/ModulesActivity.java @@ -0,0 +1,132 @@ +package com.topjohnwu.magisk; + +import android.annotation.SuppressLint; +import android.app.Activity; +import android.app.ProgressDialog; +import android.content.Context; +import android.os.AsyncTask; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.ListView; +import android.widget.TextView; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +public class ModulesActivity extends Activity { + + private static final String MAGISK_PATH = "/magisk"; + private static final String MAGISK_CACHE_PATH = "/cache/magisk"; + + private List listModules = new ArrayList<>(); + private ListView mListView; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_modules); + + mListView = (ListView) findViewById(android.R.id.list); + + new CheckFolders().execute(); + } + + private class CheckFolders extends AsyncTask { + + private ProgressDialog progress; + + @Override + protected void onPreExecute() { + super.onPreExecute(); + + progress = ProgressDialog.show(ModulesActivity.this, null, getString(R.string.loading), true, false); + } + + @Override + protected Boolean doInBackground(Void... voids) { + File[] magisk = new File(MAGISK_PATH).listFiles(File::isDirectory); + File[] magiskCache = new File(MAGISK_CACHE_PATH).listFiles(File::isDirectory); + + if (magisk != null) { + for (File mod : magisk) { + listModules.add(new Module(mod)); + } + } + + if (magiskCache != null) { + for (File mod : magiskCache) { + listModules.add(new Module(mod)); + } + } + + //noinspection Convert2streamapi + for (Module module : listModules) { + if (module.isValid()) try { + module.parse(); + } catch (Exception ignored) { + } + } + + return true; + } + + @Override + protected void onPostExecute(Boolean aBoolean) { + super.onPostExecute(aBoolean); + + progress.dismiss(); + + mListView.setAdapter(new ModulesAdapter(ModulesActivity.this, R.layout.row)); + } + } + + private class ModulesAdapter extends ArrayAdapter { + + public ModulesAdapter(Context context, int resource) { + super(context, resource); + } + + @SuppressLint("SetTextI18n") + @Override + public View getView(int position, View convertView, ViewGroup parent) { + ViewHolder vh; + + if (convertView == null) { + LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); + convertView = inflater.inflate(R.layout.row, null); + + vh = new ViewHolder(); + vh.name = (TextView) convertView.findViewById(R.id.name); + vh.version = (TextView) convertView.findViewById(R.id.version); + vh.versionCode = (TextView) convertView.findViewById(R.id.versionCode); + vh.description = (TextView) convertView.findViewById(R.id.description); + vh.cache = (TextView) convertView.findViewById(R.id.cache); + + convertView.setTag(vh); + } else { + vh = (ViewHolder) convertView.getTag(); + } + + Module module = getItem(position); + vh.name.setText("name= " + module.getName()); + vh.version.setText("version= " + module.getVersion()); + vh.versionCode.setText("versioncode= " + module.getVersionCode()); + vh.description.setText("description= " + module.getDescription()); + vh.cache.setText("is from cache= " + module.isCache()); + + return convertView; + } + + private class ViewHolder { + public TextView name; + public TextView version; + public TextView versionCode; + public TextView description; + public TextView cache; + } + } +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 86c8bb8d8..d36a1d85a 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,127 +1,131 @@ + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:focusableInTouchMode="true" + tools:context="com.topjohnwu.magisk.MainActivity"> + android:layout_marginTop="10dp" + android:text="@string/magisk_label" + android:textSize="20sp"/> - - - - + android:textStyle="bold"/> + + + + + android:text="@string/selinux_label" + android:textSize="20sp"/> + android:layout_marginLeft="10dp" + android:layout_toEndOf="@+id/selinux_label" + android:text="@string/unavailable" + android:textSize="20sp" + android:textStyle="bold"/> + android:layout_marginTop="10dp" + android:checked="false" + android:switchPadding="20dp" + android:text="@string/root_toggle" + android:textSize="20sp"/> + android:layout_marginTop="20dp" + android:checked="false" + android:switchPadding="20dp" + android:text="@string/selinux_toggle" + android:textSize="20sp"/> + +