Skip to content
Snippets Groups Projects
Commit a5a1d565 authored by nuntius35's avatar nuntius35
Browse files

Add information about last wrong PIN/last shutdown to main screen

parent 13481f04
No related branches found
No related tags found
No related merge requests found
......@@ -11,6 +11,7 @@ import android.preference.EditTextPreference;
import android.preference.PreferenceManager;
import android.preference.PreferenceFragment;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
static final int ACTIVATION_REQUEST = 1;
......@@ -41,6 +42,26 @@ public class MainActivity extends AppCompatActivity {
}
sm.testRoot();
updateStats(sm);
}
@Override
public void onResume() {
super.onResume();
sm = new ShutdownManager(context);
updateStats(sm);
}
public void updateStats(ShutdownManager sm) {
String lastWrong = sm.getLastWrong();
String lastShutdown = sm.getLastShutdown();
TextView textWrong = (TextView) findViewById(R.id.lastWrong);
textWrong.setText(getString(R.string.lastWrong));
textWrong.append(lastWrong);
TextView textShutdown = (TextView) findViewById(R.id.lastShutdown);
textShutdown.setText(getString(R.string.lastShutdown));
textShutdown.append(lastShutdown);
}
public static class MyPreferenceFragment extends PreferenceFragment
......
......@@ -3,11 +3,15 @@ package org.nuntius35.wrongpinshutdown;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import java.util.Calendar;
import java.util.Date;
class ShutdownManager {
private static final String TRIES_KEY = "tries";
private static final String MAX_TRIES_KEY = "max_tries";
private static final String SHUTDOWN_KEY = "shutdown_cmd";
private static final String LAST_WRONG_KEY = "last_wrong";
private static final String LAST_SHUTDOWN_KEY = "last_shutdown";
private int tries;
private int max_tries;
......@@ -29,29 +33,41 @@ class ShutdownManager {
void incTries() {
tries++;
sharedPref.edit().putInt(TRIES_KEY, tries).apply();
Date currentTime = Calendar.getInstance().getTime();
sharedPref.edit().putString(LAST_WRONG_KEY,currentTime.toString()).apply();
max_tries = Integer.parseInt(sharedPref.getString(MAX_TRIES_KEY, "2"));
if (tries > max_tries) {
resetTries();
shutdownDevice();
}
resetTries();
sharedPref.edit().putString(LAST_SHUTDOWN_KEY,currentTime.toString()).apply();
shutdownDevice();
}
}
private void shutdownDevice() {
try {
Process process = Runtime.getRuntime().exec(
new String[] { "su", "-c", shutdown_cmd});
process.waitFor();
new String[] { "su", "-c", shutdown_cmd});
process.waitFor();
} catch (Exception ex) {
ex.printStackTrace();
}
}
void testRoot() {
try {
Process process = Runtime.getRuntime().exec(
new String[] {"su", "-c", "ls"});
process.waitFor();
new String[] {"su", "-c", "ls"});
process.waitFor();
} catch (Exception ex) {
ex.printStackTrace();
}
}
String getLastWrong() {
return sharedPref.getString(LAST_WRONG_KEY, "–");
}
String getLastShutdown() {
return sharedPref.getString(LAST_SHUTDOWN_KEY, "–");
}
}
......@@ -8,7 +8,7 @@
android:orientation="vertical" >
<TextView
android:id="@+id/textView"
android:id="@+id/textDescription"
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
......@@ -20,5 +20,22 @@
class="org.nuntius35.wrongpinshutdown.MainActivity$MyPreferenceFragment"
android:layout_width="fill_parent"
android:layout_height="250dp" />
<TextView
android:id="@+id/lastWrong"
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="@string/lastWrong" />
<TextView
android:id="@+id/lastShutdown"
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="@string/lastShutdown" />
</LinearLayout>
</ScrollView>
......@@ -9,4 +9,6 @@
<string name="settingAdvancedDialog">Den Befehl zum Abschalten verändern</string>
<string name="settingShutdownCMD">Befehl zum Abschalten</string>
<string name="shutdownHOWTO">Hier kann der Abschaltbefehl festegelegt werden.\nDie Standardeinstellung lautet \'reboot -p\'.\nEine andere Möglichkeit ist eventuell \'svc power shutdown\'.</string>
<string name="lastWrong" >Letzter falscher PIN: \n</string>
<string name="lastShutdown" >Letztes Abschalten aufgrund einer falschen PIN: \n</string>
</resources>
......@@ -9,4 +9,6 @@
<string name="settingAdvancedDialog">Changez la commande pour arrêter le portable.</string>
<string name="settingShutdownCMD">Commande pour arrêter le portable</string>
<string name="shutdownHOWTO">Ici on peut changer la commande d\'arrêter.\nLa commande standarde: \'reboot -p\'.\nUne autre suggestion: \'svc power shutdown\'.</string>
<string name="lastWrong" >Dernier NIP faux: \n</string>
<string name="lastShutdown" >Dernier arrête dû aux NIP faux: \n</string>
</resources>
......@@ -10,4 +10,6 @@
<string name="settingShutdownCMD">Shutdown command</string>
<string name="shutdownHOWTO">Here you can specify which command is executed as root to shutdown the device.\nThe default value is \'reboot -p\'.\nAnother option might be \'svc power shutdown\'.</string>
<string name="shutdownCMD" translatable="false">reboot -p</string>
<string name="lastWrong"> Last wrong PIN: \n</string>
<string name="lastShutdown"> Last shutdown due to wrong PIN: \n</string>
</resources>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment