1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2025-01-07 10:42:07 +00:00

Add Database Size status label to Settings/About (fixes #155) (#172)

* Add new label syncthing_database_size on Settings/About

* Add Util#runShellCommandGetOutput

* Make INDEX_DB_FOLDER constant public

* Add Database Size status label to Settings/About (fixes #155)

* Imported translation de-DE

* Fix lint: Application Context within SettingsFragment
This commit is contained in:
Catfriend1 2018-12-31 16:57:51 +01:00 committed by GitHub
parent c36efcf038
commit e6eaae06f0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 88 additions and 3 deletions

View file

@ -119,6 +119,7 @@ public class SettingsActivity extends SyncthingActivity {
private static final String KEY_ST_RESET_DELTAS = "st_reset_deltas";
// Settings/About
private static final String KEY_SYNCTHING_API_KEY = "syncthing_api_key";
private static final String KEY_SYNCTHING_DATABASE_SIZE = "syncthing_database_size";
@Inject NotificationHandler mNotificationHandler;
@Inject SharedPreferences mPreferences;
@ -157,6 +158,7 @@ public class SettingsActivity extends SyncthingActivity {
private Preference mSyncthingVersion;
private Preference mSyncthingApiKey;
private Context mContext;
private SyncthingService mSyncthingService;
private RestApi mRestApi;
@ -184,6 +186,7 @@ public class SettingsActivity extends SyncthingActivity {
*/
@Override
public void onActivityCreated(Bundle savedInstanceState) {
mContext = getActivity().getApplicationContext();
super.onActivityCreated(savedInstanceState);
addPreferencesFromResource(R.xml.app_settings);
@ -310,6 +313,7 @@ public class SettingsActivity extends SyncthingActivity {
Log.d(TAG, "Failed to get app version name");
}
mSyncthingApiKey.setOnPreferenceClickListener(this);
screen.findPreference(KEY_SYNCTHING_DATABASE_SIZE).setSummary(getDatabaseSize());
openSubPrefScreen(screen);
}
@ -639,7 +643,7 @@ public class SettingsActivity extends SyncthingActivity {
return true;
case KEY_SYNCTHING_API_KEY:
// Copy syncthing's API key to clipboard.
ClipboardManager clipboard = (ClipboardManager) getActivity().getApplicationContext().getSystemService(Context.CLIPBOARD_SERVICE);
ClipboardManager clipboard = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText(getString(R.string.syncthing_api_key), mSyncthingApiKey.getSummary());
clipboard.setPrimaryClip(clip);
Toast.makeText(getActivity(), R.string.api_key_copied_to_clipboard, Toast.LENGTH_SHORT)
@ -819,5 +823,21 @@ public class SettingsActivity extends SyncthingActivity {
return false;
}
}
/**
* Calculates the size of the syncthing database on disk.
*/
private String getDatabaseSize() {
String dbPath = mContext.getFilesDir() + "/" + Constants.INDEX_DB_FOLDER;
String result = Util.runShellCommandGetOutput("/system/bin/du -sh " + dbPath, false);
if (TextUtils.isEmpty(result)) {
return "N/A";
}
String resultParts[] = result.split("\\s+");
if (resultParts.length == 0) {
return "N/A";
}
return resultParts[0];
}
}
}

View file

@ -146,7 +146,7 @@ public class Constants {
/**
* Name of the folder containing the index database.
*/
static final String INDEX_DB_FOLDER = "index-v0.14.0.db";
public static final String INDEX_DB_FOLDER = "index-v0.14.0.db";
/**
* Name of the public HTTPS CA file in the data directory.

View file

@ -12,12 +12,16 @@ import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;
import com.google.common.base.Charsets;
import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.service.Constants;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.File;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.DecimalFormat;
@ -195,7 +199,7 @@ public class Util {
shellOut.close();
}
} catch (IOException e) {
Log.w(TAG, "Failed to close shell stream", e);
Log.w(TAG, "runShellCommand: Failed to close stream", e);
}
if (shellProc != null) {
shellProc.destroy();
@ -204,6 +208,55 @@ public class Util {
return exitCode;
}
public static String runShellCommandGetOutput(String cmd, Boolean useRoot) {
int exitCode = 255;
String capturedStdOut = "";
Process shellProc = null;
DataOutputStream shellOut = null;
try {
shellProc = Runtime.getRuntime().exec((useRoot) ? "su" : "sh");
shellOut = new DataOutputStream(shellProc.getOutputStream());
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(shellOut));
Log.d(TAG, "runShellCommandGetOutput: " + cmd);
bufferedWriter.write(cmd);
bufferedWriter.flush();
shellOut.close();
shellOut = null;
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new InputStreamReader(shellProc.getInputStream(), Charsets.UTF_8));
String line;
while ((line = bufferedReader.readLine()) != null) {
// Log.i(TAG, "runShellCommandGetOutput: " + line);
capturedStdOut = capturedStdOut + line + "\n";
}
} catch (IOException e) {
Log.w(TAG, "runShellCommandGetOutput: Failed to read output", e);
} finally {
if (bufferedReader != null)
bufferedReader.close();
}
exitCode = shellProc.waitFor();
Log.i(TAG, "runShellCommandGetOutput: Exited with code " + exitCode);
} catch (IOException | InterruptedException e) {
Log.w(TAG, "runShellCommandGetOutput: Exception", e);
} finally {
try {
if (shellOut != null) {
shellOut.close();
}
} catch (IOException e) {
Log.w(TAG, "runShellCommandGetOutput: Failed to close shell stream", e);
}
if (shellProc != null) {
shellProc.destroy();
}
}
// Return captured command line output.
return capturedStdOut;
}
/**
* Make sure that dialog is showing and activity is valid before dismissing dialog, to prevent
* various crashes.

View file

@ -569,6 +569,9 @@ Bitte melden Sie auftretende Probleme via GitHub.</string>
<!-- Shown when the API key is copied to the clipboard -->
<string name="api_key_copied_to_clipboard">Syncthing API Key in die Zwischenablage kopiert</string>
<!-- Title of the preference showing database size -->
<string name="syncthing_database_size">Syncthing Datenbankgröße</string>
<!-- FolderPickerAcitivity -->

View file

@ -577,6 +577,9 @@ Please report any problems you encounter via Github.</string>
<!-- Shown when the API key is copied to the clipboard -->
<string name="api_key_copied_to_clipboard">Syncthing API key copied to clipboard</string>
<!-- Title of the preference showing database size -->
<string name="syncthing_database_size">Syncthing Database Size</string>
<!-- FolderPickerAcitivity -->

View file

@ -298,6 +298,12 @@
android:key="syncthing_api_key"
android:title="@string/syncthing_api_key" />
<Preference
android:persistent="false"
android:selectable="false"
android:key="syncthing_database_size"
android:title="@string/syncthing_database_size" />
</PreferenceScreen>
</PreferenceScreen>