Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add normal activity #7

Merged
merged 2 commits into from
Nov 19, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions gen/net/syntaxblitz/plucklock/R.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,25 @@ public static final class attr {
public static final class drawable {
public static final int ic_launcher=0x7f020000;
}
public static final class id {
public static final int pref_threshold_edit=0x7f070001;
public static final int prefs_threshold_label=0x7f070002;
public static final int textView1=0x7f070000;
}
public static final class layout {
public static final int settingsactivity=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040000;
public static final int prefs_threshold=0x7f040001;
public static final int prefs_threshold_description=0x7f040002;
public static final int app_name=0x7f050000;
public static final int prefs_threshold=0x7f050001;
public static final int prefs_threshold_description=0x7f050002;
public static final int too_low=0x7f050003;
}
public static final class style {
public static final int AppTheme=0x7f050000;
public static final int AppTheme=0x7f060000;
}
public static final class xml {
public static final int admin=0x7f030000;
public static final int preferences=0x7f030001;
public static final int admin=0x7f040000;
public static final int preferences=0x7f040001;
}
}
34 changes: 34 additions & 0 deletions res/layout/settingsactivity.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/prefs_threshold"
android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
android:id="@+id/pref_threshold_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10sp"
android:layout_weight="1"
android:ems="10"
android:inputType="number|numberDecimal" />

</LinearLayout>

<TextView
android:id="@+id/prefs_threshold_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/prefs_threshold_description" />

</LinearLayout>
Expand Down
1 change: 1 addition & 0 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<string name="app_name">PluckLock</string>
<string name="prefs_threshold">Threshold</string>
<string name="prefs_threshold_description">A value, in terms of g (9.81 m/s^2), that the sum of all non-gravitational forces on the phone must exceed for it to lock.</string>
<string name="too_low">That threshold is too low. Trust me.</string>
</resources>
19 changes: 17 additions & 2 deletions src/net/syntaxblitz/plucklock/AccelerometerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
Expand Down Expand Up @@ -45,13 +46,27 @@ public void onSensorChanged(SensorEvent event) {
if (AccelerometerService.dead)
return;

double threshold = Double.valueOf(PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getString("threshold_pref_key", "1"));
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
float threshold;

try {
threshold = prefs.getFloat("threshold_pref_key", 1);
if (threshold < .15) { // only possible pre-update.
threshold = 1;
prefs.edit().putFloat("threshold_pref_key", threshold).commit();
}
} catch (ClassCastException e) {
// The user has a non-float in the settings! Probably because they're migrating from an old version of the app.
String thresholdStr = prefs.getString("threshold_pref_key", "1");
threshold = Float.valueOf(thresholdStr);
prefs.edit().putFloat("threshold_pref_key", threshold).commit();
}
double x = Math.abs(event.values[0] / 9.81);
double y = Math.abs(event.values[1] / 9.81);
double z = Math.abs(event.values[2] / 9.81);
double sum = x + y + z;
Log.i("PluckLock", "" + sum);
if (sum > threshold && threshold > .15) {
if (sum > threshold) {
KeyguardManager keyguardManager = (KeyguardManager) getBaseContext().getSystemService(Context.KEYGUARD_SERVICE);
if (!keyguardManager.inKeyguardRestrictedInputMode()) {
DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
Expand Down
64 changes: 43 additions & 21 deletions src/net/syntaxblitz/plucklock/SettingsActivity.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
package net.syntaxblitz.plucklock;

import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.IntentFilter;
import android.content.DialogInterface.OnShowListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.PreferenceActivity;
import android.text.InputType;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.preference.PreferenceManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.Toast;

public class SettingsActivity extends PreferenceActivity {
public class SettingsActivity extends Activity {

private SharedPreferences prefs;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.settingsactivity);

// API < 10
setTheme(android.R.style.Theme);
// API > 11
Expand All @@ -33,22 +34,43 @@ public void onCreate(Bundle savedInstanceState) {
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, adminComponent);
startActivity(intent);

Intent accelerometerIntent = new Intent(getBaseContext(), AccelerometerService.class);

Intent accelerometerIntent = new Intent(getBaseContext(),
AccelerometerService.class);
getBaseContext().startService(accelerometerIntent);

this.addPreferencesFromResource(R.xml.preferences);

EditTextPreference pref = ((EditTextPreference) this.getPreferenceScreen().getPreference(0));
pref.getEditText().setInputType(InputType.TYPE_CLASS_PHONE);
pref.getEditText().setOnFocusChangeListener(new OnFocusChangeListener() {
this.prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

final EditText thresholdEdit = (EditText) this
.findViewById(R.id.pref_threshold_edit);
thresholdEdit.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable arg0) {
}

@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
}

@Override
public void onFocusChange(View arg0, boolean arg1) {
Toast.makeText(getBaseContext(), R.string.prefs_threshold_description, 8).show();
public void onTextChanged(CharSequence s, int start, int before,
int count) {
try {
float newVal = Float.valueOf(s.toString());
if (newVal < .15) {
thresholdEdit.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));
Toast.makeText(getBaseContext(), getResources().getString(R.string.too_low), Toast.LENGTH_SHORT).show();
} else {
thresholdEdit.setBackgroundColor(getResources().getColor(android.R.color.white));
SharedPreferences.Editor editor = prefs.edit();
editor.putFloat("threshold_pref_key", newVal);
editor.commit();
}
} catch (NumberFormatException e) {
thresholdEdit.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));
}
}

});
}

}