Browse Source

android various fixes and improv

pull/1410/head
kote 5 years ago
parent
commit
9d3b38141a
  1. 1
      android/res/values-ru/strings.xml
  2. 1
      android/res/values/strings.xml
  3. 89
      android/src/org/purplei2p/i2pd/I2PDActivity.java

1
android/res/values-ru/strings.xml

@ -24,4 +24,5 @@
<string name="battery_optimizations_enabled_dialog" >Ваша операционная система осуществляет оптимизации расхода аккумулятора, которые могут приводить к выгрузке I2PD из памяти и прекращению его работы с целью сэкономить заряд аккумулятора.\n\nВам сейчас будет предложено разрешить отключение этих оптимизаций.</string> <string name="battery_optimizations_enabled_dialog" >Ваша операционная система осуществляет оптимизации расхода аккумулятора, которые могут приводить к выгрузке I2PD из памяти и прекращению его работы с целью сэкономить заряд аккумулятора.\n\nВам сейчас будет предложено разрешить отключение этих оптимизаций.</string>
<string name="continue_str">Продолжить</string> <string name="continue_str">Продолжить</string>
<string name="os_version_does_not_support_battery_optimizations_show_os_dialog_api">Ваша версия Андроид не поддерживает показ диалога об оптимизациях аккумулятора для приложений.</string> <string name="os_version_does_not_support_battery_optimizations_show_os_dialog_api">Ваша версия Андроид не поддерживает показ диалога об оптимизациях аккумулятора для приложений.</string>
<string name="shutdown_canceled">Плановая остановка отменена</string>
</resources> </resources>

1
android/res/values/strings.xml

@ -24,4 +24,5 @@
<string name="device_does_not_support_disabling_battery_optimizations">Your Android version does not support opting out of battery optimizations</string> <string name="device_does_not_support_disabling_battery_optimizations">Your Android version does not support opting out of battery optimizations</string>
<string name="menu_item_battery_optimizations_str">Battery Optimizations</string> <string name="menu_item_battery_optimizations_str">Battery Optimizations</string>
<string name="os_version_does_not_support_battery_optimizations_show_os_dialog_api">Your Android OS version does not support showing the dialog for battery optimizations for applications.</string> <string name="os_version_does_not_support_battery_optimizations_show_os_dialog_api">Your Android OS version does not support showing the dialog for battery optimizations for applications.</string>
<string name="shutdown_canceled">Planned shutdown canceled</string>
</resources> </resources>

89
android/src/org/purplei2p/i2pd/I2PDActivity.java

@ -44,7 +44,6 @@ import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
// For future package update checking // For future package update checking
import org.purplei2p.i2pd.BuildConfig;
import static android.provider.Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS; import static android.provider.Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS;
@ -86,6 +85,7 @@ public class I2PDActivity extends Activity {
}; };
private static volatile long graceStartedMillis; private static volatile long graceStartedMillis;
private static final Object graceStartedMillis_LOCK=new Object(); private static final Object graceStartedMillis_LOCK=new Object();
private Menu optionsMenu;
private static String formatGraceTimeRemaining() { private static String formatGraceTimeRemaining() {
long remainingSeconds; long remainingSeconds;
@ -138,7 +138,7 @@ public class I2PDActivity extends Activity {
super.onDestroy(); super.onDestroy();
textView = null; textView = null;
daemon.removeStateChangeListener(daemonStateUpdatedListener); daemon.removeStateChangeListener(daemonStateUpdatedListener);
//cancelGracefulStop(); //cancelGracefulStop0();
try{ try{
doUnbindService(); doUnbindService();
}catch(Throwable tr){ }catch(Throwable tr){
@ -160,7 +160,7 @@ public class I2PDActivity extends Activity {
} }
} }
private static void cancelGracefulStop() { private void cancelGracefulStop0() {
Timer gracefulQuitTimer = getGracefulQuitTimer(); Timer gracefulQuitTimer = getGracefulQuitTimer();
if(gracefulQuitTimer!=null) { if(gracefulQuitTimer!=null) {
gracefulQuitTimer.cancel(); gracefulQuitTimer.cancel();
@ -232,6 +232,7 @@ public class I2PDActivity extends Activity {
// Inflate the menu; this adds items to the action bar if it is present. // Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.options_main, menu); getMenuInflater().inflate(R.menu.options_main, menu);
menu.findItem(R.id.action_battery_otimizations).setVisible(isBatteryOptimizationsOpenOsDialogApiAvailable()); menu.findItem(R.id.action_battery_otimizations).setVisible(isBatteryOptimizationsOpenOsDialogApiAvailable());
this.optionsMenu = menu;
return true; return true;
} }
@ -251,15 +252,11 @@ public class I2PDActivity extends Activity {
i2pdStop(); i2pdStop();
return true; return true;
case R.id.action_graceful_stop: case R.id.action_graceful_stop:
if (getGracefulQuitTimer()!= null) synchronized (graceStartedMillis_LOCK) {
{ if (getGracefulQuitTimer() != null)
item.setTitle(R.string.action_graceful_stop); cancelGracefulStop();
i2pdCancelGracefulStop (); else
} i2pdGracefulStop();
else
{
item.setTitle(R.string.action_cancel_graceful_stop);
i2pdGracefulStop();
} }
return true; return true;
case R.id.action_battery_otimizations: case R.id.action_battery_otimizations:
@ -282,7 +279,7 @@ public class I2PDActivity extends Activity {
} }
private void i2pdStop() { private void i2pdStop() {
cancelGracefulStop(); cancelGracefulStop0();
new Thread(() -> { new Thread(() -> {
Log.d(TAG, "stopping"); Log.d(TAG, "stopping");
try { try {
@ -329,16 +326,17 @@ public class I2PDActivity extends Activity {
},"gracInit").start(); },"gracInit").start();
} }
private void i2pdCancelGracefulStop() private void cancelGracefulStop()
{ {
cancelGracefulStop(); cancelGracefulStop0();
Toast.makeText(this, R.string.startedOkay, Toast.LENGTH_SHORT).show();
new Thread(() -> { new Thread(() -> {
try try
{ {
Log.d(TAG, "grac stopping cancel"); Log.d(TAG, "canceling grac stop");
if(daemon.isStartedOkay()) if(daemon.isStartedOkay()) {
daemon.startAcceptingTunnels(); daemon.startAcceptingTunnels();
runOnUiThread(() -> Toast.makeText(this, R.string.shutdown_canceled, Toast.LENGTH_SHORT).show());
}
else else
i2pdStop(); i2pdStop();
} }
@ -374,8 +372,19 @@ public class I2PDActivity extends Activity {
return gracefulQuitTimer; return gracefulQuitTimer;
} }
private static void setGracefulQuitTimer(Timer gracefulQuitTimer) { private void setGracefulQuitTimer(Timer gracefulQuitTimer) {
I2PDActivity.gracefulQuitTimer = gracefulQuitTimer; I2PDActivity.gracefulQuitTimer = gracefulQuitTimer;
runOnUiThread(()-> {
Menu menu = optionsMenu;
if (menu != null) {
MenuItem item = menu.findItem(R.id.action_graceful_stop);
if (item != null) {
synchronized (graceStartedMillis_LOCK) {
item.setTitle(getGracefulQuitTimer() != null ? R.string.action_cancel_graceful_stop : R.string.action_graceful_stop);
}
}
}
});
} }
/** /**
@ -398,20 +407,22 @@ public class I2PDActivity extends Activity {
// to a file. That doesn't appear to be the case. If the returned array is // to a file. That doesn't appear to be the case. If the returned array is
// null or has 0 length, we assume the path is to a file. This means empty // null or has 0 length, we assume the path is to a file. This means empty
// directories will get turned into files. // directories will get turned into files.
if (contents == null || contents.length == 0) if (contents == null || contents.length == 0) {
throw new IOException(); copyFileAsset(path);
return;
}
// Make the directory. // Make the directory.
File dir = new File(i2pdpath, path); File dir = new File(i2pdpath, path);
Log.d(TAG, "dir.mkdirs() returned "+dir.mkdirs()); boolean result = dir.mkdirs();
Log.d(TAG, "dir.mkdirs() returned " + result);
// Recurse on the contents. // Recurse on the contents.
for (String entry : contents) { for (String entry : contents) {
copyAsset(path + "/" + entry); copyAsset(path + '/' + entry);
} }
} catch (IOException e) { } catch (IOException e) {
Log.e(TAG,"ex ignored", e); Log.e(TAG, "ex ignored for path='" + path + "'", e);
copyFileAsset(path);
} }
} }
@ -424,19 +435,21 @@ public class I2PDActivity extends Activity {
*/ */
private void copyFileAsset(String path) { private void copyFileAsset(String path) {
File file = new File(i2pdpath, path); File file = new File(i2pdpath, path);
if(!file.exists()) try { if(!file.exists()) {
InputStream in = getAssets().open(path); try {
OutputStream out = new FileOutputStream(file); try (InputStream in = getAssets().open(path) ) {
byte[] buffer = new byte[1024]; try (OutputStream out = new FileOutputStream(file)) {
int read = in.read(buffer); byte[] buffer = new byte[1024];
while (read != -1) { int read = in.read(buffer);
out.write(buffer, 0, read); while (read != -1) {
read = in.read(buffer); out.write(buffer, 0, read);
read = in.read(buffer);
}
}
}
} catch (IOException e) {
Log.e(TAG, "", e);
} }
out.close();
in.close();
} catch (IOException e) {
Log.e(TAG, "", e);
} }
} }

Loading…
Cancel
Save