150 lines
5.7 KiB
Java
150 lines
5.7 KiB
Java
package com.localtransfer;
|
|
|
|
import android.Manifest;
|
|
import android.app.Activity;
|
|
import android.content.Intent;
|
|
import android.content.SharedPreferences;
|
|
import android.content.pm.PackageManager;
|
|
import android.os.Bundle;
|
|
import android.os.Environment;
|
|
import android.util.Log;
|
|
import android.view.MenuItem;
|
|
|
|
import androidx.appcompat.app.ActionBar;
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
import androidx.core.app.ActivityCompat;
|
|
import androidx.core.content.ContextCompat;
|
|
import androidx.preference.Preference;
|
|
import androidx.preference.PreferenceFragmentCompat;
|
|
import androidx.preference.PreferenceManager;
|
|
import androidx.preference.SwitchPreference;
|
|
|
|
public class SettingsActivity extends AppCompatActivity {
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
setContentView(R.layout.settings_activity);
|
|
getSupportFragmentManager()
|
|
.beginTransaction()
|
|
.replace(R.id.settings, new SettingsFragment())
|
|
.commit();
|
|
ActionBar actionBar = getSupportActionBar();
|
|
if (actionBar != null) {
|
|
actionBar.setDisplayHomeAsUpEnabled(true);
|
|
}
|
|
}
|
|
|
|
public static class SettingsFragment extends PreferenceFragmentCompat {
|
|
|
|
private final static int REQUEST_DIRECTORY_PICKER = 4000;
|
|
static Preference directory;
|
|
|
|
@Override
|
|
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
|
|
setPreferencesFromResource(R.xml.root_preferences, rootKey);
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
|
|
|
|
directory = findPreference("shared_storage");
|
|
directory.setSummary(prefs.getString("shared_storage", null));
|
|
|
|
SwitchPreference shared;
|
|
shared = findPreference("use_shared_storage");
|
|
if(shared.isChecked()) {
|
|
if(checkAndRequestPermissions())
|
|
directory.setVisible(true);
|
|
else
|
|
shared.setChecked(false);
|
|
}
|
|
else
|
|
directory.setVisible(false);
|
|
|
|
shared.setOnPreferenceClickListener(preference -> {
|
|
if(shared.isChecked()) {
|
|
if(checkAndRequestPermissions())
|
|
directory.setVisible(true);
|
|
else
|
|
shared.setChecked(false);
|
|
}
|
|
else
|
|
directory.setVisible(false);
|
|
return false;
|
|
});
|
|
directory.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
|
|
@Override
|
|
public boolean onPreferenceClick(Preference preference) {
|
|
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
|
|
intent.addCategory(Intent.CATEGORY_DEFAULT);
|
|
startActivityForResult(intent, REQUEST_DIRECTORY_PICKER);
|
|
return false;
|
|
}
|
|
});
|
|
}
|
|
|
|
private boolean checkAndRequestPermissions() {
|
|
|
|
if (ContextCompat.checkSelfPermission(
|
|
getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE) ==
|
|
PackageManager.PERMISSION_GRANTED) {
|
|
Log.d("READ_EXTERNAL_STORAGE", "already granted");
|
|
return true;
|
|
}
|
|
else {
|
|
ActivityCompat.requestPermissions(getActivity(), new String[] {android.Manifest.permission.READ_EXTERNAL_STORAGE}, MainActivity.REQUEST_ID_READ_EXTERNAL_STORAGE);
|
|
}
|
|
return false;
|
|
}
|
|
@Override
|
|
public void onRequestPermissionsResult(int requestCode,
|
|
String permissions[],
|
|
int[] grantResults) {
|
|
switch (requestCode) {
|
|
case MainActivity.REQUEST_ID_READ_EXTERNAL_STORAGE:
|
|
if (grantResults.length > 0 &&
|
|
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
|
Log.d("READ_EXTERNAL_STORAGE", "granted");
|
|
} else {
|
|
Log.d("READ_EXTERNAL_STORAGE", "No granted");
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
switch (requestCode) {
|
|
case REQUEST_DIRECTORY_PICKER:
|
|
if (resultCode == Activity.RESULT_OK) {
|
|
String currentPath = data.getExtras().getString("data");
|
|
if(currentPath.contains(Environment.getExternalStorageDirectory() + "/")) {
|
|
String save_location = currentPath.replace(Environment.getExternalStorageDirectory() + "/", "");
|
|
Log.d("Path", save_location);
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
|
|
prefs.edit().putString("shared_storage", save_location).apply();
|
|
directory.setSummary(save_location);
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected void onDestroy() {
|
|
super.onDestroy();
|
|
Transfer.parameter(this);
|
|
}
|
|
|
|
@Override
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
int id = item.getItemId();
|
|
|
|
if (id == android.R.id.home) {
|
|
onBackPressed(); return true;
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
}
|
|
} |