Added area settings, moved profile and wifi setting to area.

This commit is contained in:
Felix Ableitner 2012-12-06 13:20:52 +01:00
parent 652fb0f7c3
commit 37de69ae63
13 changed files with 242 additions and 102 deletions

View file

@ -27,6 +27,9 @@
</activity>
<activity
android:name=".activities.AreaEdit" />
<activity
android:name=".activities.ProfileList"
android:label="@string/profilelist_label" />

View file

@ -16,8 +16,8 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:title="@string/arealist_profile_choose"
android:id="@+id/choose" />
<item android:title="@string/arealist_edit"
android:id="@+id/edit" />
<item android:title="@string/arealist_rename"
android:id="@+id/rename" />

View file

@ -31,12 +31,16 @@
<string name="arealist_intro_title">Welcome</string>
<string name="arealist_intro_text">This app automatically activates different profiles when you enter certain areas.\n\nTo get started, choose or create an area you are currently at, long click on it and choose \'learn\'. Select the interval you were or will be in the area.\n\nThe app will then apply the associated profile whenever you enter the area (note that inaccuracies of multiple kilometers/miles may occur).\n\nYou can see the current area and profile in the notification.</string>
<string name="arealist_profile_none">None</string>
<string name="arealist_profile_choose">Choose Profile</string>
<string name="arealist_learn">Learn Area</string>
<string name="arealist_rename">Rename Area</string>
<string name="arealist_delete">Delete Area</string>
<string name="arealist_delete_message">Do you really want to delete this area?</string>
<string name="arealist_new">New Area</string>
<string name="arealist_edit">Edit Area</string>
<!-- AreaEdit -->
<string name="areaedit_profile">Profile</string>
<string name="areaedit_wifi">Wifi Enabled</string>
<string-array name="arealist_learn_area_strings">
<item>Next 6 hours</item>
@ -72,7 +76,6 @@
<string name="profileedit_media">Media Volume</string>
<string name="profileedit_alarm">Alarm Volume</string>
<string name="profileedit_ringer">Ringer Mode</string>
<string name="profileedit_wifi">Wifi Enabled</string>
<string-array name="profileedit_ringer_mode_strings">
<item>Keep previous</item>
<item>Normal</item>

29
res/xml/area_edit.xml Normal file
View file

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2012 Felix Ableitner
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<Preference
android:key="profile"
android:title="@string/areaedit_profile"
android:persistent="false" />
<CheckBoxPreference
android:key="wifi_enabled"
android:title="@string/areaedit_wifi"
android:persistent="false" />
</PreferenceScreen>

View file

@ -43,9 +43,4 @@
android:entryValues="@array/profileedit_ringer_mode_values"
android:persistent="false" />
<CheckBoxPreference
android:key="wifi_enabled"
android:title="@string/profileedit_wifi"
android:persistent="false" />
</PreferenceScreen>

View file

@ -25,6 +25,7 @@ import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.media.AudioManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
@ -210,20 +211,26 @@ public class LocationService extends Service {
// Get area name and ID of the associated profile.
Cursor c = db.getReadableDatabase().query(
AreaColumns.TABLE_NAME + " as a, " + CellColumns.TABLE_NAME,
new String[] { AreaColumns.PROFILE_ID, AreaColumns.NAME },
"a." + AreaColumns._ID + " = " + CellColumns.AREA_ID + " AND " +
CellColumns.CELL_ID + " = ? AND " +
CellColumns.CELL_TYPE + " = ?",
new String[] { Integer.toString(mCurrentCell),
Integer.toString(mNetworkType)},
AreaColumns.TABLE_NAME + " as a, " + CellColumns.TABLE_NAME + " as c",
new String[] { "a." + AreaColumns.PROFILE_ID, "a." + AreaColumns.NAME,
"a." + AreaColumns.WIFI_ENABLED },
"a." + AreaColumns._ID + " = c." + CellColumns.AREA_ID + " AND " +
"c." + CellColumns.CELL_ID + " = ? AND " +
"c." + CellColumns.CELL_TYPE + " = ?",
new String[] { Integer.toString(cell),
Integer.toString(mNetworkType) },
null, null, null);
long profileId = Database.ROW_NONE;
String areaName;
if (c.moveToFirst()) {
profileId = c.getLong(c.getColumnIndex(AreaColumns.PROFILE_ID));
areaName = c.getString(c.getColumnIndex(AreaColumns.NAME));
WifiManager wm = (WifiManager) LocationService.this
.getSystemService(Context.WIFI_SERVICE);
wm.setWifiEnabled((c.getInt(c.getColumnIndex(AreaColumns.WIFI_ENABLED)) == 1)
? true : false);
}
else {
areaName = getResources().getString(
@ -237,7 +244,6 @@ public class LocationService extends Service {
ProfileColumns.NOTIFICATION_VOLUME,
ProfileColumns.MEDIA_VOLUME,
ProfileColumns.ALARM_VOLUME,
ProfileColumns.WIFI_ENABLED,
ProfileColumns.RINGER_MODE },
"_id = ?",
new String[] { Long.toString(profileId) },
@ -246,11 +252,8 @@ public class LocationService extends Service {
// Apply profile if there is one and set the name for the notification.
String profileName;
if (c.moveToFirst()) {
Context context = LocationService.this;
AudioManager am = (AudioManager) context
AudioManager am = (AudioManager) LocationService.this
.getSystemService(Context.AUDIO_SERVICE);
WifiManager wm = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
// Value smaller zero means the volume should not change.
if (c.getInt(c.getColumnIndex(ProfileColumns.RINGTONE_VOLUME)) >= 0) {
@ -281,9 +284,6 @@ public class LocationService extends Service {
ProfileColumns.RINGER_MODE_KEEP) {
am.setRingerMode(c.getInt(c.getColumnIndex(ProfileColumns.RINGER_MODE)));
}
wm.setWifiEnabled((c.getInt(c.getColumnIndex(ProfileColumns.WIFI_ENABLED)) == 0)
? true : false);
profileName = c.getString(c.getColumnIndex(ProfileColumns.NAME));
}

View file

@ -0,0 +1,160 @@
/*
* Copyright (C) 2012 Felix Ableitner
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.nutomic.pegasus.activities;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.util.Log;
import android.util.Pair;
import com.github.nutomic.pegasus.LocationService;
import com.github.nutomic.pegasus.R;
import com.github.nutomic.pegasus.content.AreaColumns;
import com.github.nutomic.pegasus.content.Database;
import com.github.nutomic.pegasus.content.ProfileColumns;
/**
* Allows editing an area, uses Database values (or defaults) to
* initialize, saves to Database.
*
* Using deprecated methods because v4 support library does not have
* PreferenceFragment implementation.
*
* @author Felix Ableitner
*
*/
public class AreaEdit extends PreferenceActivity implements
OnPreferenceChangeListener, OnPreferenceClickListener {
/** Key for the Intent extra to store the area edited here. */
public static final String AREA_ID = "area_id";
private long mArea;
private Preference mProfile;
private CheckBoxPreference mWifi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mArea = getIntent().getExtras().getLong(AREA_ID);
addPreferencesFromResource(R.xml.area_edit);
new AsyncTask<Void, Void, Cursor>() {
@Override
protected Cursor doInBackground(Void... params) {
return Database.getInstance(AreaEdit.this).getReadableDatabase()
.query(AreaColumns.TABLE_NAME,
new String[] { AreaColumns.WIFI_ENABLED },
AreaColumns._ID + " = ?",
new String[] { Long.toString(mArea) },
null, null, null);
}
@Override
protected void onPostExecute(Cursor c) {
c.moveToFirst();
// TODO: nothing here works
mProfile = findPreference("profile");
mProfile.setOnPreferenceClickListener(AreaEdit.this);
mWifi = (CheckBoxPreference) findPreference("wifi_enabled");
mWifi.setChecked((c.getInt(c.getColumnIndex(AreaColumns.WIFI_ENABLED)) == 1)
? true : false);
mWifi.setOnPreferenceChangeListener(AreaEdit.this);
}
}.execute((Void) null);
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
ContentValues cv = new ContentValues();
if (preference.equals(mWifi)) {
cv.put(AreaColumns.WIFI_ENABLED, (Boolean) newValue);
}
else if (preference.equals(mProfile)) {
// TODO: does not cause profile to be applied
cv.put(AreaColumns.PROFILE_ID, (Long) newValue);
}
final ContentValues values = cv;
new Thread(new Runnable() {
@Override
public void run() {
Database.getInstance(AreaEdit.this).getWritableDatabase()
.update(AreaColumns.TABLE_NAME,
values,
AreaColumns._ID + "= ?",
new String[] { Long.toString(mArea) });
LocationService.sendUpdateIntent(AreaEdit.this);
}
}).start();
return true;
}
/**
* Show a "pick sound profile" dialog for an area, the selected profile is
* then set to be launched when entering the area.
*/
@Override
public boolean onPreferenceClick(Preference preference) {
final Database db = Database.getInstance(this);
// Get a pair of all area names and all area IDs.
Cursor c = db.getReadableDatabase().query(
ProfileColumns.TABLE_NAME,
new String[] { ProfileColumns._ID,
ProfileColumns.NAME },
null, null, null, null, null);
String[] names = new String[c.getCount()];
Long[] ids = new Long[c.getCount()];
while (c.moveToNext()) {
ids[c.getPosition()] = c.getLong(c.getColumnIndex(ProfileColumns._ID));
names[c.getPosition()] = c.getString(c.getColumnIndex(ProfileColumns.NAME));
}
final Pair<String[], Long[]> p = Pair.create(names, ids);
new AlertDialog.Builder(this).setTitle(R.string.areaedit_profile)
.setItems(p.first, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, final int which) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
onPreferenceChange(mProfile, p.second[which]);
return null;
}
}.execute((Void) null);
}
}).show();
return true;
}
}

View file

@ -30,7 +30,6 @@ import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Pair;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
@ -153,55 +152,9 @@ public class AreaList extends ListActivity {
*/
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
chooseProfile(id);
}
/**
* Show a "pick sound profile" dialog for an area, the selected profile is
* then set to be launched when entering the area.
*
* @param area
* The sqlite id of the area to choose a profile for. Set -1 if
* the area does not exist and should be created.
*/
private void chooseProfile(final long area) {
final Database db = Database.getInstance(this);
// Get a pair of all area names and all area IDs.
Cursor c = db.getReadableDatabase().query(
ProfileColumns.TABLE_NAME,
new String[] { ProfileColumns._ID,
ProfileColumns.NAME },
null, null, null, null, null);
String[] names = new String[c.getCount()];
Long[] ids = new Long[c.getCount()];
while (c.moveToNext()) {
ids[c.getPosition()] = c.getLong(c.getColumnIndex(ProfileColumns._ID));
names[c.getPosition()] = c.getString(c.getColumnIndex(ProfileColumns.NAME));
}
final Pair<String[], Long[]> p = Pair.create(names, ids);
new AlertDialog.Builder(this).setTitle(R.string.arealist_profile_choose)
.setItems(p.first, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, final int which) {
new UpdateTask() {
@Override
protected Long doInBackground(Void... params) {
// Set the new profile in area.
ContentValues cv = new ContentValues();
cv.put(AreaColumns.PROFILE_ID, p.second[which]);
db.getWritableDatabase().update(AreaColumns.TABLE_NAME,
cv,
AreaColumns._ID + " = ?",
new String[] { Long.toString(area) });
return null;
}
}.execute((Void) null);
}
}).show();
Intent i = new Intent(this, AreaEdit.class);
i.putExtra(AreaEdit.AREA_ID, id);
startActivity(i);
}
/**
@ -240,9 +193,6 @@ public class AreaList extends ListActivity {
.getMenuInfo();
final Database db = Database.getInstance(this);
switch (item.getItemId()) {
case R.id.choose:
chooseProfile(info.id);
return true;
case R.id.rename:
renameArea(info.id, getAreaName((AdapterContextMenuInfo) item.getMenuInfo()));
return true;

View file

@ -22,7 +22,6 @@ import android.database.Cursor;
import android.media.AudioManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
@ -35,7 +34,7 @@ import com.github.nutomic.pegasus.content.Database;
import com.github.nutomic.pegasus.content.ProfileColumns;
/**
* Allows editing a sound profile, uses Database values (or defaults) to
* Allows editing a profile, uses Database values (or defaults) to
* initialize, saves to Database.
*
* Using deprecated methods because v4 support library does not have
@ -56,7 +55,6 @@ public class ProfileEdit extends PreferenceActivity implements
private VolumePreference mMediaVolume;
private VolumePreference mAlarmVolume;
private ListPreference mRingerMode;
private CheckBoxPreference mWifiEnabled;
/**
* Initialize sound profile id from intent (extra "profile_id" must
@ -68,7 +66,7 @@ public class ProfileEdit extends PreferenceActivity implements
mProfile = getIntent().getExtras().getLong(PROFILE_ID);
addPreferencesFromResource(R.xml.profile);
addPreferencesFromResource(R.xml.profile_edit);
new AsyncTask<Void, Void, Cursor>() {
@ -81,7 +79,6 @@ public class ProfileEdit extends PreferenceActivity implements
ProfileColumns.NOTIFICATION_VOLUME,
ProfileColumns.MEDIA_VOLUME,
ProfileColumns.ALARM_VOLUME,
ProfileColumns.WIFI_ENABLED,
ProfileColumns.RINGER_MODE },
"_id = ?",
new String[] { Long.toString(mProfile) },
@ -118,11 +115,6 @@ public class ProfileEdit extends PreferenceActivity implements
mRingerMode = (ListPreference) findPreference("ringer_mode");
mRingerMode.setValue(Integer.toString(c.getInt(c.getColumnIndex(ProfileColumns.RINGER_MODE))));
mRingerMode.setOnPreferenceChangeListener(ProfileEdit.this);
mWifiEnabled = (CheckBoxPreference) findPreference("wifi_enabled");
mWifiEnabled.setChecked((c.getInt(c.getColumnIndex(ProfileColumns.WIFI_ENABLED)) == 0)
? true : false);
mWifiEnabled.setOnPreferenceChangeListener(ProfileEdit.this);
};
@ -149,9 +141,6 @@ public class ProfileEdit extends PreferenceActivity implements
else if (preference.equals(mRingerMode)) {
cv.put(ProfileColumns.RINGER_MODE, Integer.parseInt((String) newValue));
}
else if (preference.equals(mWifiEnabled)) {
cv.put(ProfileColumns.RINGTONE_VOLUME, (Boolean) newValue);
}
final ContentValues values = cv;
new Thread(new Runnable() {

View file

@ -256,11 +256,10 @@ public class ProfileList extends ListActivity {
ContentValues cv = new ContentValues();
cv.put(ProfileColumns.NAME, getResources()
.getString(R.string.profilelist_new));
cv.put(ProfileColumns.RINGTONE_VOLUME, 5);
cv.put(ProfileColumns.NOTIFICATION_VOLUME, 5);
cv.put(ProfileColumns.MEDIA_VOLUME, 5);
cv.put(ProfileColumns.ALARM_VOLUME, 5);
cv.put(ProfileColumns.WIFI_ENABLED, true);
cv.put(ProfileColumns.RINGTONE_VOLUME, 5 - ProfileColumns.VOLUME_APPLY_FALSE);
cv.put(ProfileColumns.NOTIFICATION_VOLUME, 5 - ProfileColumns.VOLUME_APPLY_FALSE);
cv.put(ProfileColumns.MEDIA_VOLUME, 10 - ProfileColumns.VOLUME_APPLY_FALSE);
cv.put(ProfileColumns.ALARM_VOLUME, 5 - ProfileColumns.VOLUME_APPLY_FALSE);
cv.put(ProfileColumns.RINGER_MODE, AudioManager.RINGER_MODE_NORMAL);
return Database.getInstance(ProfileList.this).getWritableDatabase()

View file

@ -33,11 +33,13 @@ public class AreaColumns implements BaseColumns {
// Columns
public static final String NAME = "name";
public static final String PROFILE_ID = "profile_id";
public static final String WIFI_ENABLED = "wifi_enabled";
public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" +
_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
NAME + " TEXT," +
PROFILE_ID + " INTEGER" +
PROFILE_ID + " INTEGER, " +
WIFI_ENABLED + " INTEGER " +
");";
}

View file

@ -38,7 +38,7 @@ public class Database extends SQLiteOpenHelper {
private static final String TAG = "Database";
private static final String DATABASE_NAME = "pegasus.db";
private static final int DATABASE_VERSION = 1;
private static final int DATABASE_VERSION = 2;
private static Database mInstance = null;
@ -83,7 +83,6 @@ public class Database extends SQLiteOpenHelper {
cv.put(ProfileColumns.NOTIFICATION_VOLUME, 5 - ProfileColumns.VOLUME_APPLY_FALSE);
cv.put(ProfileColumns.MEDIA_VOLUME, 9 - ProfileColumns.VOLUME_APPLY_FALSE);
cv.put(ProfileColumns.ALARM_VOLUME, 5 - ProfileColumns.VOLUME_APPLY_FALSE);
cv.put(ProfileColumns.WIFI_ENABLED, true);
cv.put(ProfileColumns.RINGER_MODE, ProfileColumns.RINGER_MODE_KEEP);
long normal = db.insert(ProfileColumns.TABLE_NAME, null, cv);
@ -96,7 +95,6 @@ public class Database extends SQLiteOpenHelper {
cv.put(ProfileColumns.NOTIFICATION_VOLUME, 0);
cv.put(ProfileColumns.MEDIA_VOLUME, 0 - ProfileColumns.VOLUME_APPLY_FALSE);
cv.put(ProfileColumns.ALARM_VOLUME, 0 - ProfileColumns.VOLUME_APPLY_FALSE);
cv.put(ProfileColumns.WIFI_ENABLED, true);
cv.put(ProfileColumns.RINGER_MODE, AudioManager.RINGER_MODE_VIBRATE);
long silent = db.insert(ProfileColumns.TABLE_NAME, null, cv);
@ -105,6 +103,7 @@ public class Database extends SQLiteOpenHelper {
cv.put(AreaColumns.NAME, mContext.getResources()
.getString(R.string.sql_area_unknown));
cv.put(AreaColumns.PROFILE_ID, normal);
cv.put(AreaColumns.WIFI_ENABLED, false);
db.insert(AreaColumns.TABLE_NAME, null, cv);
// Insert "Home" area.
@ -112,6 +111,7 @@ public class Database extends SQLiteOpenHelper {
cv.put(AreaColumns.NAME, mContext.getResources()
.getString(R.string.sql_area_home));
cv.put(AreaColumns.PROFILE_ID, normal);
cv.put(AreaColumns.WIFI_ENABLED, true);
db.insert(AreaColumns.TABLE_NAME, null, cv);
// Insert "Work" area.
@ -119,11 +119,23 @@ public class Database extends SQLiteOpenHelper {
cv.put(AreaColumns.NAME, mContext.getResources()
.getString(R.string.sql_area_work));
cv.put(AreaColumns.PROFILE_ID, silent);
cv.put(AreaColumns.WIFI_ENABLED, true);
db.insert(AreaColumns.TABLE_NAME, null, cv);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Move wifi preference from profile to area.
if (oldVersion == 1) {
db.rawQuery("ALTER TABLE " + ProfileColumns.TABLE_NAME +
" DROP wifi_enabled", null);
db.rawQuery("ALTER TABLE " + AreaColumns.TABLE_NAME +
" ADD " + AreaColumns.WIFI_ENABLED + " INTEGER", null);
ContentValues cv = new ContentValues();
cv.put(AreaColumns.WIFI_ENABLED, true);
db.update(AreaColumns.TABLE_NAME, cv, null, null);
}
}
}

View file

@ -34,7 +34,6 @@ public class ProfileColumns implements BaseColumns {
public static final String NOTIFICATION_VOLUME = "notification_volume";
public static final String MEDIA_VOLUME = "media_volume";
public static final String ALARM_VOLUME = "alarm_volume";
public static final String WIFI_ENABLED = "wifi_enabled";
public static final String RINGER_MODE = "ringer_mode";
@ -45,7 +44,6 @@ public class ProfileColumns implements BaseColumns {
NOTIFICATION_VOLUME + " INTEGER," +
MEDIA_VOLUME + " INTEGER," +
ALARM_VOLUME + " INTEGER," +
WIFI_ENABLED + " INTEGER," +
RINGER_MODE + " INTEGER" +
");";