Skip to content

Commit

Permalink
Add import and export settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Dev4Mod committed May 11, 2024
1 parent 5e34f98 commit d30a660
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,29 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;
import androidx.preference.PreferenceManager;

import com.wmods.wppenhacer.App;
import com.wmods.wppenhacer.BuildConfig;
import com.wmods.wppenhacer.FilePicker;
import com.wmods.wppenhacer.MainActivity;
import com.wmods.wppenhacer.R;
import com.wmods.wppenhacer.databinding.FragmentHomeBinding;
import com.wmods.wppenhacer.ui.fragments.base.BaseFragment;
import com.wmods.wppenhacer.xposed.core.MainFeatures;

import org.json.JSONObject;

import java.util.Arrays;
import java.util.HashSet;

import rikka.core.util.IOUtils;

public class HomeFragment extends BaseFragment {

Expand All @@ -33,6 +41,7 @@ public class HomeFragment extends BaseFragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

var intentFilter = new IntentFilter(BuildConfig.APPLICATION_ID + ".RECEIVER_WPP");
ContextCompat.registerReceiver(requireContext(), new BroadcastReceiver() {

Expand Down Expand Up @@ -69,6 +78,7 @@ private void receiverBroadcastWpp(Context context, Intent intent) {
binding.statusTitle2.setText(R.string.whatsapp_in_background);
var version = intent.getStringExtra("VERSION");
var supported_list = Arrays.asList(context.getResources().getStringArray(R.array.supported_versions_wpp));

if (supported_list.contains(version)) {
binding.statusSummary1.setText(String.format(getString(R.string.version_s), version));
binding.status2.setCardBackgroundColor(context.getColor(rikka.material.R.color.material_green_500));
Expand Down Expand Up @@ -100,10 +110,69 @@ public View onCreateView(@NonNull LayoutInflater inflater,
disableBusiness(requireActivity());
});

binding.exportBtn.setOnClickListener(view -> saveConfigs(this.getContext()));
binding.importBtn.setOnClickListener(view -> importConfigs(this.getContext()));

return binding.getRoot();
}

private void saveConfigs(Context context) {
FilePicker.setOnUriPickedListener((uri) -> {
try {
try (var output = context.getContentResolver().openOutputStream(uri)) {
var prefs = PreferenceManager.getDefaultSharedPreferences(context);
var entries = prefs.getAll();
var JSOjsonObject = new JSONObject();
for (var entry : entries.entrySet()) {
JSOjsonObject.put(entry.getKey(), entry.getValue());
}
output.write(JSOjsonObject.toString().getBytes());
}
Toast.makeText(context, context.getString(R.string.configs_saved), Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
FilePicker.fileSalve.launch("wpp_enhacer_config.json");
}

private void importConfigs(Context context) {
FilePicker.setOnUriPickedListener((uri) -> {
try {
try (var input = context.getContentResolver().openInputStream(uri)) {
var data = IOUtils.toString(input);
var prefs = PreferenceManager.getDefaultSharedPreferences(context);
var jsonObject = new JSONObject(data);
prefs.getAll().forEach((key, value) -> prefs.edit().remove(key).apply());
var key = jsonObject.keys();
while (key.hasNext()) {
var keyName = key.next();
var value = jsonObject.get(keyName);
if (value instanceof String stringValue) {
if (stringValue.startsWith("[") && stringValue.endsWith("]")) {
if (stringValue.length() > 2) {
prefs.edit().putStringSet(keyName, new HashSet<>(Arrays.asList(stringValue.substring(1, stringValue.length() - 1).split(",")))).apply();
}
} else {
prefs.edit().putString(keyName, value.toString()).apply();
}
} else if (value instanceof Boolean) {
prefs.edit().putBoolean(keyName, (boolean) value).apply();
} else if (value instanceof Integer) {
prefs.edit().putInt(keyName, (int) value).apply();
} else if (value instanceof Long) {
prefs.edit().putLong(keyName, (long) value).apply();
}
}
}
Toast.makeText(context, context.getString(R.string.configs_imported), Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
FilePicker.fileCapture.launch(new String[]{"application/json"});
}

private void checkStateWpp(FragmentActivity activity) {

if (MainActivity.isXposedEnabled()) {
Expand Down
47 changes: 47 additions & 0 deletions app/src/main/res/layout/fragment_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,53 @@
</LinearLayout>

</com.google.android.material.card.MaterialCardView>

<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_marginTop="16dp"
android:layout_height="wrap_content">

<LinearLayout
android:padding="?dialogPreferredPadding"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical">

<com.google.android.material.textview.MaterialTextView
style="@style/DeviceInfoDialogLabel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="@string/backup_settings" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="horizontal">

<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:id="@+id/importBtn"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_weight="1"
android:text="@string/import_settings" />

<com.google.android.material.button.MaterialButton
android:id="@+id/exportBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="8dp"
android:text="@string/export_settings" />
</LinearLayout>
</LinearLayout>

</com.google.android.material.card.MaterialCardView>


</LinearLayout>
</rikka.widget.borderview.BorderNestedScrollView>
</FrameLayout>
Expand Down

0 comments on commit d30a660

Please sign in to comment.