settings password protected

This commit is contained in:
Milos Kozak 2017-02-14 19:16:34 +01:00
parent 5992413cf9
commit 2e4aec9916
9 changed files with 338 additions and 174 deletions

View file

@ -39,6 +39,7 @@ import info.nightscout.androidaps.tabs.TabPageAdapter;
import info.nightscout.utils.LogDialog;
import info.nightscout.utils.ImportExportPrefs;
import info.nightscout.utils.LocaleHelper;
import info.nightscout.utils.PasswordProtection;
public class MainActivity extends AppCompatActivity {
private static Logger log = LoggerFactory.getLogger(MainActivity.class);
@ -125,8 +126,13 @@ public class MainActivity extends AppCompatActivity {
int id = item.getItemId();
switch (id) {
case R.id.nav_preferences:
Intent i = new Intent(getApplicationContext(), PreferencesActivity.class);
startActivity(i);
PasswordProtection.QueryPassword(this, R.string.settings_password, "settings_password", new Runnable() {
@Override
public void run() {
Intent i = new Intent(getApplicationContext(), PreferencesActivity.class);
startActivity(i);
}
}, null);
break;
case R.id.nav_resetdb:
new AlertDialog.Builder(this)

View file

@ -55,7 +55,7 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre
}
if (pref instanceof EditTextPreference) {
EditTextPreference editTextPref = (EditTextPreference) pref;
if (pref.getKey().contains("danar_password")) {
if (pref.getKey().contains("password")) {
pref.setSummary("******");
} else if (editTextPref.getText() != null && !editTextPref.getText().equals("")) {
((EditTextPreference) pref).setDialogMessage(editTextPref.getDialogMessage());
@ -83,6 +83,7 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_password);
addPreferencesFromResource(R.xml.pref_quickwizard);
addPreferencesFromResource(R.xml.pref_language);
if (Config.CAREPORTALENABLED)

View file

@ -9,7 +9,9 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
@ -28,6 +30,7 @@ import info.nightscout.androidaps.interfaces.ProfileInterface;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.plugins.NSProfile.NSProfilePlugin;
import info.nightscout.androidaps.plugins.VirtualPump.VirtualPumpPlugin;
import info.nightscout.utils.PasswordProtection;
public class ConfigBuilderFragment extends Fragment implements FragmentBase {
@ -62,6 +65,8 @@ public class ConfigBuilderFragment extends Fragment implements FragmentBase {
PluginCustomAdapter constraintsDataAdapter = null;
PluginCustomAdapter generalDataAdapter = null;
LinearLayout mainLayout;
Button unlock;
// TODO: sorting
@ -89,6 +94,25 @@ public class ConfigBuilderFragment extends Fragment implements FragmentBase {
if (ConfigBuilderPlugin.nightscoutVersionCode < 900)
nightscoutVerView.setTextColor(Color.RED);
setViews();
unlock = (Button) view.findViewById(R.id.configbuilder_unlock);
mainLayout = (LinearLayout) view.findViewById(R.id.configbuilder_mainlayout);
if (PasswordProtection.isLocked("settings_password")) {
mainLayout.setVisibility(View.GONE);
unlock.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PasswordProtection.QueryPassword(getContext(), R.string.settings_password, "settings_password", new Runnable() {
@Override
public void run() {
mainLayout.setVisibility(View.VISIBLE);
unlock.setVisibility(View.GONE);
}
}, null);
}
});
}
return view;
}

View file

@ -0,0 +1,72 @@
package info.nightscout.utils;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
/**
* Created by mike on 14.02.2017.
*/
public class PasswordProtection {
static public boolean isLocked(String preference) {
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
final String password = SP.getString(preference, "");
if (password.equals("")) {
return false;
}
return true;
}
static public void QueryPassword(final Context context, int stringID, String preference, final Runnable ok, final Runnable fail) {
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
final String password = SP.getString(preference, "");
if (password.equals("")) {
if (ok != null) ok.run();
return;
}
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.passwordprompt, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setView(promptsView);
final TextView label = (TextView) promptsView.findViewById(R.id.passwordprompt_text);
label.setText(MainApp.sResources.getString(stringID));
final EditText userInput = (EditText) promptsView.findViewById(R.id.passwordprompt_pass);
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
String enteredPassword = userInput.getText().toString();
if (password.equals(enteredPassword)) {
if (ok != null) ok.run();
} else {
ToastUtils.showToastInUiThread(context, MainApp.sResources.getString(R.string.wrongpassword));
if (fail != null) fail.run();
}
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}

View file

@ -13,194 +13,208 @@
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/configbuilder_unlock"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/unlock_settings" />
<LinearLayout
android:orientation="horizontal"
android:id="@+id/configbuilder_mainlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="@string/configbuilder_bgsource"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/configbuilder_nsclientversion_label" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/configbuilder_nsclientversion"
android:layout_marginLeft="10dp" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp">
android:textStyle="bold" />
<ListView
android:id="@+id/configbuilder_bgsourcelistview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/cardColorBackground" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="@string/configbuilder_pump"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/configbuilder_nightscoutversion_label" />
android:textStyle="bold" />
<ListView
android:id="@+id/configbuilder_pumplistview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/cardColorBackground" />
<TextView
android:id="@+id/configbuilder_apslabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="@string/configbuilder_aps"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<ListView
android:id="@+id/configbuilder_apslistview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/cardColorBackground" />
<TextView
android:id="@+id/configbuilder_looplabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="@string/configbuilder_loop"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<ListView
android:id="@+id/configbuilder_looplistview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/cardColorBackground" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/configbuilder_nightscoutversion"
android:layout_marginLeft="10dp" />
android:layout_marginLeft="5dp"
android:text="@string/configbuilder_profile"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<ListView
android:id="@+id/configbuilder_profilelistview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/cardColorBackground" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="@string/configbuilder_treatments"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<ListView
android:id="@+id/configbuilder_treatmentslistview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/cardColorBackground" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="@string/configbuilder_tempbasals"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<ListView
android:id="@+id/configbuilder_tempslistview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/cardColorBackground" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="@string/configbuilder_constraints"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<ListView
android:id="@+id/configbuilder_constraintslistview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/cardColorBackground" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="@string/configbuilder_general"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<ListView
android:id="@+id/configbuilder_generallistview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/cardColorBackground" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/configbuilder_nsclientversion_label"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/configbuilder_nsclientversion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/configbuilder_nightscoutversion_label"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/configbuilder_nightscoutversion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="@string/configbuilder_bgsource"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<ListView
android:id="@+id/configbuilder_bgsourcelistview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/cardColorBackground" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="@string/configbuilder_pump"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<ListView
android:id="@+id/configbuilder_pumplistview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/cardColorBackground" />
<TextView
android:id="@+id/configbuilder_apslabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="@string/configbuilder_aps"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<ListView
android:id="@+id/configbuilder_apslistview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/cardColorBackground" />
<TextView
android:id="@+id/configbuilder_looplabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="@string/configbuilder_loop"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<ListView
android:id="@+id/configbuilder_looplistview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/cardColorBackground" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="@string/configbuilder_profile"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<ListView
android:id="@+id/configbuilder_profilelistview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/cardColorBackground" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="@string/configbuilder_treatments"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<ListView
android:id="@+id/configbuilder_treatmentslistview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/cardColorBackground" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="@string/configbuilder_tempbasals"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<ListView
android:id="@+id/configbuilder_tempslistview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/cardColorBackground" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="@string/configbuilder_constraints"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<ListView
android:id="@+id/configbuilder_constraintslistview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/cardColorBackground" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="@string/configbuilder_general"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<ListView
android:id="@+id/configbuilder_generallistview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/cardColorBackground" />
</LinearLayout>
</ScrollView>
</FrameLayout>

View file

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="@+id/passwordprompt_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/passwordprompt_pass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword">
<requestFocus />
</EditText>
</LinearLayout>

View file

@ -472,4 +472,9 @@
<string name="updatingbasalrates">Aktualizuji bazály</string>
<string name="bolusrequested">Spouštím dodávání %.2fU</string>
<string name="waitingforpump">Čekání na pumpu</string>
<string name="settings_password">Heslo do nastavení</string>
<string name="virtualpump_settings">Nastavení virtuální pumpy</string>
<string name="virtualpump_uploadstatus_title">Nahrávat status do NS</string>
<string name="wrongpassword">Chybné heslo</string>
<string name="unlock_settings">Odemknout nastavení</string>
</resources>

View file

@ -503,4 +503,7 @@
<string name="executing">Executing</string>
<string name="virtualpump_settings">Virtual pump settings</string>
<string name="virtualpump_uploadstatus_title">Upload status to NS</string>
<string name="wrongpassword">Wrong password</string>
<string name="settings_password">Password for settings</string>
<string name="unlock_settings">Unlock settings</string>
</resources>

View file

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:key="password"
android:title="@string/settings_password">
<EditTextPreference
android:title="@string/settings_password"
android:key="settings_password"
android:inputType="textPassword">
</EditTextPreference>
</PreferenceCategory>
</PreferenceScreen>