Skip to content

Commit

Permalink
Improved using latest root file picker
Browse files Browse the repository at this point in the history
Signed-off-by: sunilpaulmathew <sunil.kde@gmail.com>
  • Loading branch information
sunilpaulmathew committed Jan 1, 2023
1 parent 08aa0a1 commit 036dc3b
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 86 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ dependencies {
implementation 'androidx.preference:preference:1.2.0'
implementation 'com.github.ozodrukh:CircularReveal:1.3.1'
implementation 'com.github.sunilpaulmathew:sCommon:b293ec9813'
implementation 'com.github.sunilpaulmathew:RootFilePicker:e8a410b248'
implementation 'com.github.sunilpaulmathew:RootFilePicker:c918e8fad0'
implementation 'com.github.topjohnwu.libsu:core:4.0.3'
implementation "com.github.topjohnwu.libsu:io:3.1.2"
implementation 'com.google.android.apps.dashclock:dashclock-api:2.0.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package com.smartpack.kernelmanager.activities.tools.profile;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
Expand Down Expand Up @@ -186,7 +187,7 @@ private void returnIntent(LinkedHashMap<String, String> commandsList) {
intent.putExtra(POSITION_INTENT, mProfilePosition);
intent.putExtra(RESULT_ID_INTENT, ids);
intent.putExtra(RESULT_COMMAND_INTENT, commands);
setResult(0, intent);
setResult(Activity.RESULT_OK, intent);
finish();
} else {
Utils.snackbar(findViewById(android.R.id.content), getString(R.string.no_changes));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package com.smartpack.kernelmanager.activities.tools.profile;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

Expand Down Expand Up @@ -79,7 +80,7 @@ private Fragment getFragment() {
@Override
public void finish() {
if (sChanged) {
setResult(0, new Intent());
setResult(Activity.RESULT_OK, new Intent());
}
sChanged = false;
super.finish();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.smartpack.kernelmanager.views.recyclerview.SelectView;
import com.smartpack.kernelmanager.views.recyclerview.SwitchView;

import java.io.File;
import java.util.List;

import in.sunilpaulmathew.sCommon.Utils.sExecutor;
Expand Down Expand Up @@ -471,7 +472,7 @@ private void showCreateDialog() {
if (text.contains(" ")) {
text = text.replace(" ", "_");
}
if (Utils.existFile(KLapse.profileFolder() + "/" + text)) {
if (Utils.existFile(KLapse.profileFolder(requireActivity()) + "/" + text)) {
Utils.snackbar(getRootView(), getString(R.string.profile_exists, text));
return;
}
Expand All @@ -485,25 +486,22 @@ public void onPreExecute() {
}
@Override
public void doInBackground() {
KLapse.prepareProfileFolder();
Utils.create("#!/system/bin/sh\n\n# Created by SmartPack-Kernel Manager", KLapse.profileFolder() + "/" + path);
if (KLapse.supported()) {
Utils.append("\n# K-lapse", KLapse.profileFolder() + "/" + path);
for (int i = 0; i < KLapse.size(); i++) {
KLapse.exportKlapseSettings(path, i);
}
if (KLapse.profileFolder(requireActivity()).exists()) {
KLapse.profileFolder(requireActivity()).delete();
}
Utils.append("\n# The END\necho \"Profile applied successfully...\" | tee /dev/kmsg", KLapse.profileFolder() + "/" + path);
Utils.create(KLapse.klapseSettings(), new File(KLapse.profileFolder(requireActivity()), path).getAbsolutePath());
Utils.prepareInternalDataStorage();
Utils.copy(new File(KLapse.profileFolder(requireActivity()), path).getAbsolutePath(), Utils.getInternalDataStorage() + "/" + path);
}
@Override
public void onPostExecute() {
hideProgressMessage();
new Dialog(requireActivity())
.setMessage(getString(R.string.profile_created, KLapse.profileFolder() + "/" + path))
.setMessage(getString(R.string.profile_created, KLapse.profileFolder(requireActivity()) + "/" + path))
.setCancelable(false)
.setNegativeButton(getString(R.string.cancel), (dialog, id) -> {
})
.setPositiveButton(getString(R.string.share), (dialog, id) -> Utils.shareItem(getActivity(), path, KLapse.profileFolder() + "/" + path, getString(R.string.share_script)
.setPositiveButton(getString(R.string.share), (dialog, id) -> Utils.shareItem(getActivity(), path, Utils.getInternalDataStorage() + "/" + path, getString(R.string.share_script)
+ "\n\n" + getString(R.string.share_app_message, BuildConfig.VERSION_NAME)))
.show();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package com.smartpack.kernelmanager.fragments.tools;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
Expand Down Expand Up @@ -229,12 +230,11 @@ protected void onTopFabClick() {
showBackupFlashingDialog(null);
break;
case 1:
new FilePicker(
"img",
Environment.getExternalStorageDirectory().toString(),
ViewUtils.getThemeAccentColor(requireContext()),
flashImage,
requireActivity()).launch();
FilePicker filePicker = new FilePicker(flashImage, requireActivity());
filePicker.setExtension("img");
filePicker.setPath(Environment.getExternalStorageDirectory().toString());
filePicker.setAccentColor(ViewUtils.getThemeAccentColor(requireContext()));
filePicker.launch();
break;
}
}).setOnDismissListener(dialogInterface -> mOptionsDialog = null);
Expand Down Expand Up @@ -379,7 +379,7 @@ private LinkedHashMap<String, Backup.PARTITION> getPartitionMenu() {
ActivityResultLauncher<Intent> flashImage = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getData() != null && FilePicker.getSelectedFile().exists()) {
if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null && FilePicker.getSelectedFile().exists()) {
File mSelectedFile = FilePicker.getSelectedFile();
if (!Utils.getExtension(mSelectedFile.getAbsolutePath()).equals("img")) {
Utils.snackbar(getRootView(), getString(R.string.wrong_extension, ".img"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package com.smartpack.kernelmanager.fragments.tools;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Environment;
Expand Down Expand Up @@ -148,9 +149,8 @@ private void switchItemsList(List<RecyclerViewItem> items) {
mDeleteDialog.show();
break;
case 1:
Utils.shareItem(getActivity(), switchItems, CustomControls.switchFile() + "/" +
switchItems,getString(R.string.share_controller, CustomControls.switchFile()) +
" -> Import -> Switch'.\n\n" + getString(R.string.share_app_message, BuildConfig.VERSION_NAME));
Utils.shareItem(getActivity(), switchItems, CustomControls.switchFile() + "/" + switchItems, getString(R.string.share_controller,
CustomControls.switchFile()) + " -> Import -> Switch'.\n\n" + getString(R.string.share_app_message, BuildConfig.VERSION_NAME));
break;
}
return false;
Expand Down Expand Up @@ -195,9 +195,8 @@ private void genericItemsList(List<RecyclerViewItem> items) {
mDeleteDialog.show();
break;
case 1:
Utils.shareItem(getActivity(), genericItems, CustomControls.genericFile() +
"/" + genericItems,getString(R.string.share_controller, CustomControls.genericFile()) +
" -> Import -> Generic'.\n\n" + getString(R.string.share_app_message, BuildConfig.VERSION_NAME));
Utils.shareItem(getActivity(), genericItems, CustomControls.genericFile() + "/" + genericItems, getString(R.string.share_controller,
CustomControls.genericFile()) + " -> Import -> Generic'.\n\n" + getString(R.string.share_app_message, BuildConfig.VERSION_NAME));
break;
}
return false;
Expand Down Expand Up @@ -256,19 +255,18 @@ protected void onTopFabClick() {

private void createController(int controllerType, String path) {
mControllerType = controllerType;
new FilePicker(
null,
path,
ViewUtils.getThemeAccentColor(requireContext()),
createController,
requireActivity()).launch();
FilePicker filePicker = new FilePicker(createController, requireActivity());
filePicker.setExtension(null);
filePicker.setPath(path);
filePicker.setAccentColor(ViewUtils.getThemeAccentColor(requireContext()));
filePicker.launch();
}

@SuppressLint("StringFormatInvalid")
ActivityResultLauncher<Intent> createController = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getData() != null && FilePicker.getSelectedFile().exists()) {
if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null && FilePicker.getSelectedFile().exists()) {
File mSelectedFile = FilePicker.getSelectedFile();
if (mControllerType == -1) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,12 +352,11 @@ protected void onTopFabClick() {
createProfile.launch(createProfileActivityIntent());
break;
case 1:
new FilePicker(
"json",
Environment.getExternalStorageDirectory().toString(),
ViewUtils.getThemeAccentColor(requireContext()),
importProfile,
requireActivity()).launch();
FilePicker filePicker = new FilePicker(importProfile, requireActivity());
filePicker.setExtension("json");
filePicker.setPath(Environment.getExternalStorageDirectory().toString());
filePicker.setAccentColor(ViewUtils.getThemeAccentColor(requireContext()));
filePicker.launch();
break;
}
}).setOnDismissListener(dialogInterface -> mOptionsDialog = null);
Expand Down Expand Up @@ -389,7 +388,7 @@ private Intent createProfileActivityIntent() {
ActivityResultLauncher<Intent> appendToProfile = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() != Activity.RESULT_OK && result.getData() != null) {
if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) {
LinkedHashMap<String, String> commandsList = new LinkedHashMap<>();
ArrayList<String> ids = result.getData().getStringArrayListExtra(ProfileActivity.RESULT_ID_INTENT);
ArrayList<String> commands = result.getData().getStringArrayListExtra(ProfileActivity.RESULT_COMMAND_INTENT);
Expand Down Expand Up @@ -419,7 +418,7 @@ private Intent createProfileActivityIntent() {
ActivityResultLauncher<Intent> createProfile = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() != Activity.RESULT_OK && result.getData() != null) {
if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) {
LinkedHashMap<String, String> commandsList = new LinkedHashMap<>();
ArrayList<String> ids = result.getData().getStringArrayListExtra(ProfileActivity.RESULT_ID_INTENT);
ArrayList<String> commands = result.getData().getStringArrayListExtra(ProfileActivity.RESULT_COMMAND_INTENT);
Expand All @@ -436,7 +435,7 @@ private Intent createProfileActivityIntent() {
ActivityResultLauncher<Intent> editProfile = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() != Activity.RESULT_OK && result.getData() != null) {
if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) {
reload();
}
}
Expand All @@ -445,7 +444,7 @@ private Intent createProfileActivityIntent() {
ActivityResultLauncher<Intent> importProfile = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getData() != null && FilePicker.getSelectedFile().exists()) {
if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null && FilePicker.getSelectedFile().exists()) {
File mSelectedFile = FilePicker.getSelectedFile();
if (!Utils.getExtension(mSelectedFile.getAbsolutePath()).equals("json")) {
Utils.snackbar(getRootView(), getString(R.string.wrong_extension, ".json"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
import com.smartpack.kernelmanager.BuildConfig;
import com.smartpack.kernelmanager.R;
import com.smartpack.kernelmanager.activities.ApplyScriptActivity;
import com.smartpack.kernelmanager.activities.ScriptEditorActivity;
import com.smartpack.kernelmanager.activities.ForegroundActivity;
import com.smartpack.kernelmanager.activities.ScriptEditorActivity;
import com.smartpack.kernelmanager.fragments.DescriptionFragment;
import com.smartpack.kernelmanager.fragments.RecyclerViewFragment;
import com.smartpack.kernelmanager.fragments.SwitcherFragment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package com.smartpack.kernelmanager.fragments.tools;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
Expand Down Expand Up @@ -518,19 +519,18 @@ public void onPostExecute() {
@Override
protected void onTopFabClick() {
super.onTopFabClick();
new FilePicker(
"zip",
Environment.getExternalStorageDirectory().toString(),
ViewUtils.getThemeAccentColor(requireContext()),
pickFileForFlashing,
requireActivity()).launch();
FilePicker filePicker = new FilePicker(pickFileForFlashing, requireActivity());
filePicker.setExtension("zip");
filePicker.setPath(Environment.getExternalStorageDirectory().toString());
filePicker.setAccentColor(ViewUtils.getThemeAccentColor(requireContext()));
filePicker.launch();
}

@SuppressLint({"StringFormatInvalid", "StringFormatMatches"})
ActivityResultLauncher<Intent> pickFileForFlashing = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getData() != null && FilePicker.getSelectedFile().exists()) {
if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null && FilePicker.getSelectedFile().exists()) {
File mSelectedFile = FilePicker.getSelectedFile();
new Dialog(requireActivity())
.setIcon(R.mipmap.ic_launcher)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@

import com.smartpack.kernelmanager.R;
import com.smartpack.kernelmanager.fragments.ApplyOnBootFragment;

import com.smartpack.kernelmanager.utils.Utils;
import com.smartpack.kernelmanager.utils.root.Control;
import com.topjohnwu.superuser.io.SuFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* Created by sunilpaulmathew <sunil.kde@gmail.com> on March 5, 2019
Expand Down Expand Up @@ -335,40 +335,30 @@ private static String getAdjustedTime(String string) {
"0" + timeMin : timeMin) + (timeHr > 12 ? " PM" : " AM");
}

private static final String[] KLAPSE_PROFILE = {
KLAPSE_ENABLE, KLAPSE_TARGET_R, KLAPSE_TARGET_G, KLAPSE_TARGET_B,
DAYTIME_R, DAYTIME_G, DAYTIME_B, KLAPSE_START_MIN, KLAPSE_END_MIN, KLAPSE_TARGET_MIN,
FADEBACK_MINUTES, DIMMER_FACTOR, DIMMER_FACTOR_AUTO, DIMMER_START, DIMMER_END, PULSE_FREQ,
FLOW_FREQ, BACKLIGHT_RANGE_UPPER, BACKLIGHT_RANGE_LOWER
};

public static File profileFolder() {
return SuFile.open(Utils.getInternalDataStorage(), "klapse/");
}

public static void prepareProfileFolder() {
if (profileFolder().exists() && profileFolder().isDirectory()) {
profileFolder().delete();
}
profileFolder().mkdirs();
}

public static void exportKlapseSettings(String name, int position) {
prepareProfileFolder();
String value = Utils.readFile(KLAPSE_PROFILE[position]);
if (value.contains("Y")) {
value = "1";
} else if (value.contains("N")) {
value = "0";
}
if (Utils.existFile(KLAPSE_PROFILE[position])) {
String command = "echo " + value + " > " + KLAPSE_PROFILE[position];
Utils.append (command, profileFolder() + "/" + name);
public static File profileFolder(Context context) {
return context.getExternalFilesDir("klapse");
}

public static String klapseSettings() {
StringBuilder sb = new StringBuilder();
sb.append("#!/system/bin/sh\n\n# Created by SmartPack-Kernel Manager");
if (supported()) {
sb.append("\n# K-lapse");
for (File file : Objects.requireNonNull(SuFile.open(KLAPSE).listFiles())) {
if (file.exists() && file.isFile() && !file.getName().equals("version")) {
String value = Utils.readFile(file.getAbsolutePath());
if (value.contains("Y")) {
value = "1";
} else if (value.contains("N")) {
value = "0";
}
String command = "echo " + value + " > " + file.getAbsolutePath();
sb.append(command);
}
}
}
}

public static int size() {
return KLAPSE_PROFILE.length;
sb.append("\n# The END | tee /dev/kmsg");
return sb.toString();
}

public static boolean supported() {
Expand Down

0 comments on commit 036dc3b

Please sign in to comment.