Shared Storage optional setting

This commit is contained in:
lionel 2022-02-25 17:11:23 +01:00
parent d87296a534
commit 778415cf0b
9 changed files with 92 additions and 45 deletions

2
.idea/misc.xml generated
View File

@ -5,7 +5,9 @@
<map>
<entry key="app/src/main/res/layout/file_info.xml" value="0.24479166666666666" />
<entry key="app/src/main/res/layout/layout.xml" value="0.35989583333333336" />
<entry key="app/src/main/res/layout/settings_activity.xml" value="0.215625" />
<entry key="app/src/main/res/xml/network_security_config.xml" value="0.35989583333333336" />
<entry key="app/src/main/res/xml/root_preferences.xml" value="0.35833333333333334" />
</map>
</option>
</component>

View File

@ -7,6 +7,7 @@ import android.content.Context;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.animation.Animation;
@ -14,6 +15,8 @@ import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.LinearLayout;
import androidx.core.content.FileProvider;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
@ -32,8 +35,6 @@ public class DownloadFile extends Transfer {
public String type;
public String save_location;
public Uri uri;
private Button button;
@ -60,7 +61,7 @@ public class DownloadFile extends Transfer {
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DISPLAY_NAME, name);
values.put(MediaStore.MediaColumns.MIME_TYPE, mime);
values.put(MediaStore.MediaColumns.RELATIVE_PATH, Transfer.local_storage);
values.put(MediaStore.MediaColumns.RELATIVE_PATH, Transfer.shared_storage);
uri = resolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, values);
}
try {
@ -71,7 +72,6 @@ public class DownloadFile extends Transfer {
e.printStackTrace();
state = Transfer.STATE_FAILED;
error = ExceptionName + ": " + ExceptionMess;
progressEnd();
buttonProgressEnd();
@ -83,25 +83,51 @@ public class DownloadFile extends Transfer {
}
public boolean exist(Context context) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
Cursor cursor = context.getContentResolver().query(MediaStore.Downloads.EXTERNAL_CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()) {
String MediaStore_File_name = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Downloads.DISPLAY_NAME));
long MediaStore_File_size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Downloads.SIZE));
if(MediaStore_File_name.equals(name) && MediaStore_File_size == size) {
int cursor_id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Downloads._ID));
uri = ContentUris.withAppendedId(MediaStore.Downloads.EXTERNAL_CONTENT_URI, cursor_id);
return true;
}
}
}
else {
File file = new File(save_location, name);
new File(save_location).mkdirs();
uri = Uri.fromFile(file);
if (!Transfer.use_shared_storage){
File file = new File(activity.getFilesDir(), name);
uri = FileProvider.getUriForFile(activity, activity.getPackageName(), file);
if (file.exists() && file.length() == size)
return true;
}
else {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
Cursor cursor = context.getContentResolver().query(MediaStore.Downloads.EXTERNAL_CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()) {
String MediaStore_File_name = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Downloads.DISPLAY_NAME));
long MediaStore_File_size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Downloads.SIZE));
if(MediaStore_File_name.equals(name) && MediaStore_File_size == size) {
int cursor_id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Downloads._ID));
uri = ContentUris.withAppendedId(MediaStore.Downloads.EXTERNAL_CONTENT_URI, cursor_id);
return true;
}
}
}
else {
String save_location = null;
if (mime.equals("text/plain"))
save_location = context.getCacheDir().toString();
else
save_location = Environment.getExternalStorageDirectory() + "/" + Transfer.shared_storage;
new File(save_location).mkdirs();
File file = new File(save_location, name);
try {
file.createNewFile();
} catch (IOException e) {
final String ExceptionName = e.getClass().getSimpleName();
final String ExceptionMess = e.getMessage();
e.printStackTrace();
if(ExceptionName != null && ExceptionMess != null) {
errorSnackbar(ExceptionName + ": " + ExceptionMess);
}
}
uri = Uri.fromFile(file);
if (file.exists() && file.length() == size)
return true;
}
}
return false;
}

View File

@ -81,7 +81,8 @@ public class MainActivity extends AppCompatActivity {
.putString("host", getString(R.string.server_host_def))
.putString("port", getString(R.string.server_port_def))
.putString("root", getString(R.string.server_root_def))
.putString("local_storage", getString(R.string.local_storage_def))
.putString("shared_storage", getString(R.string.shared_storage_def))
.putBoolean("use_shared_storage", false)
.apply();
Transfer.parameter(this);
@ -195,7 +196,7 @@ public class MainActivity extends AppCompatActivity {
startActivity(settings);
break;
case R.id.action_folder:
String save_location = Environment.getExternalStorageDirectory() + "/" + Transfer.local_storage;
String save_location = Environment.getExternalStorageDirectory() + "/" + Transfer.shared_storage;
Uri selectedUri = Uri.parse(save_location.toString());
boolean intentSuccess = false;
try {

View File

@ -13,6 +13,7 @@ import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;
import androidx.preference.SwitchPreference;
import lib.folderpicker.FolderPicker;
@ -42,8 +43,24 @@ public class SettingsActivity extends AppCompatActivity {
setPreferencesFromResource(R.xml.root_preferences, rootKey);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
directory = findPreference("local_storage");
directory.setSummary(prefs.getString("local_storage", null));
directory = findPreference("shared_storage");
directory.setSummary(prefs.getString("shared_storage", null));
SwitchPreference shared;
shared = findPreference("use_shared_storage");
if(shared.isChecked())
directory.setVisible(true);
else
directory.setVisible(false);
shared.setOnPreferenceClickListener(preference -> {
if(shared.isChecked())
directory.setVisible(true);
else
directory.setVisible(false);
return false;
});
directory.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
@ -65,7 +82,7 @@ public class SettingsActivity extends AppCompatActivity {
String save_location = currentPath.replace(Environment.getExternalStorageDirectory() + "/", "");
Log.d("Path", save_location);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
prefs.edit().putString("local_storage", save_location).apply();
prefs.edit().putString("shared_storage", save_location).apply();
directory.setSummary(save_location);
}
}

View File

@ -32,7 +32,6 @@ import java.sql.Timestamp;
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.ConcurrentModificationException;
import java.util.List;
@ -56,7 +55,8 @@ public class Transfer {
public static Integer port;
public static String root;
public static String protocol;
public static String local_storage;
public static String shared_storage;
public static boolean use_shared_storage;
public static Activity activity;
@ -65,7 +65,6 @@ public class Transfer {
public int drawable;
public String info;
public String message;
public String error;
public String sizeSI;
@ -127,7 +126,8 @@ public class Transfer {
host = prefs.getString("host", null);
root = prefs.getString("root", null);
local_storage = prefs.getString("local_storage", null);
shared_storage = prefs.getString("shared_storage", null);
use_shared_storage = prefs.getBoolean("use_shared_storage", false);
if(prefs.getBoolean("protocol", false))
protocol = "https";
else

View File

@ -93,7 +93,6 @@ public class UploadFile extends Transfer {
final String ExceptionMess = e.getMessage();
state = Transfer.STATE_FAILED;
error = ExceptionName + ": " + ExceptionMess;
progressEnd();
if(ExceptionName != null && ExceptionMess != null) {

View File

@ -20,6 +20,7 @@ import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.fragment.app.Fragment;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
@ -224,11 +225,6 @@ public class DownloadFragment extends Fragment {
trFile.setButton(file_buttons.findViewById(R.id.file_download));
if (trFile.mime.equals("text/plain"))
trFile.save_location = getContext().getCacheDir().toString();
else
trFile.save_location = Environment.getExternalStorageDirectory() + "/" + Transfer.local_storage;
viewName.setText(trFile.name);
viewType.setText(trFile.mime);
viewSize.setText(Transfer.humanReadableByteCountBin(trFile.size));
@ -330,6 +326,7 @@ public class DownloadFragment extends Fragment {
startActivity(Intent.createChooser(intent, getString(R.string.share_title)));
} catch (ActivityNotFoundException e) {
e.printStackTrace();
Toast.makeText(getActivity(), "Can't share " + dl.mime, Toast.LENGTH_SHORT).show();
}
}
else {
@ -341,10 +338,9 @@ public class DownloadFragment extends Fragment {
startActivity(Intent.createChooser(intent, getString(R.string.share_title)));
} catch (ActivityNotFoundException e) {
e.printStackTrace();
Toast.makeText(getActivity(), "Can't share " + dl.mime, Toast.LENGTH_SHORT).show();
}
}
};
private View.OnClickListener ListenerView = v -> {
@ -360,9 +356,8 @@ public class DownloadFragment extends Fragment {
startActivity(intent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
Toast.makeText(getActivity(), "No " + dl.mime + " viewer", Toast.LENGTH_SHORT).show();
}
};
private List<View> getAllChildren(View v) {

View File

@ -30,8 +30,9 @@
<string name="server_root_def">/transfer</string>
<!-- Local Preferences -->
<string name="local_storage">Local Storage</string>
<string name="local_storage_def">Download/TransferLocal</string>
<string name="use_shared_storage">Use shared storage</string>
<string name="shared_storage">Shared Storage</string>
<string name="shared_storage_def">Download/TransferLocal</string>
<!-- File buttons -->
<string name="file_share">Share</string>

View File

@ -31,11 +31,17 @@
<PreferenceCategory app:title="@string/local_header">
<SwitchPreference
android:defaultValue="false"
android:key="use_shared_storage"
android:title="@string/use_shared_storage" />
<Preference
app:key="local_storage"
app:title="@string/local_storage"
app:defaultValue="@string/local_storage_def"
app:useSimpleSummaryProvider="true" />
app:key="shared_storage"
app:title="@string/shared_storage"
app:defaultValue="@string/shared_storage_def"
app:useSimpleSummaryProvider="true"
app:isPreferenceVisible="false"/>
</PreferenceCategory>