diff --git a/android/FarmAlarm/app/build.gradle b/android/FarmAlarm/app/build.gradle
index bb18181..f4a9233 100644
--- a/android/FarmAlarm/app/build.gradle
+++ b/android/FarmAlarm/app/build.gradle
@@ -10,6 +10,7 @@ android {
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
+ vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
@@ -26,5 +27,7 @@ dependencies {
})
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:design:24.2.1'
+ compile 'com.android.support:support-v4:24.2.1'
+ compile 'com.android.support:support-vector-drawable:24.2.1'
testCompile 'junit:junit:4.12'
}
diff --git a/android/FarmAlarm/app/src/main/AndroidManifest.xml b/android/FarmAlarm/app/src/main/AndroidManifest.xml
index 4acbece..da2a3f7 100644
--- a/android/FarmAlarm/app/src/main/AndroidManifest.xml
+++ b/android/FarmAlarm/app/src/main/AndroidManifest.xml
@@ -3,6 +3,7 @@
package="com.zoblak.farmalarm">
+ * This class makes heavy use of the {@link NotificationCompat.Builder} helper + * class to create notifications in a backward-compatible way. + */ +public class AlarmNotification { + /** + * The unique identifier for this type of notification. + */ + private static final String NOTIFICATION_TAG = "Alarm"; + + /** + * Shows the notification, or updates a previously shown notification of + * this type, with the given parameters. + *
+ * TODO: Customize this method's arguments to present relevant content in + * the notification. + *
+ * TODO: Customize the contents of this method to tweak the behavior and + * presentation of alarm notifications. Make + * sure to follow the + * + * Notification design guidelines when doing so. + * + * @see #cancel(Context) + */ + public static void notify(final Context context, + final String exampleString, final int number) { + final Resources res = context.getResources(); + + // This image is used as the notification's large icon (thumbnail). + // TODO: Remove this if your notification has no relevant thumbnail. + final Bitmap picture = BitmapFactory.decodeResource(res, R.drawable.example_picture); + + + final String ticker = exampleString; + final String title = res.getString( + R.string.alarm_notification_title_template, exampleString); + final String text = res.getString( + R.string.alarm_notification_placeholder_text_template, exampleString); + + final NotificationCompat.Builder builder = new NotificationCompat.Builder(context) + + // Set appropriate defaults for the notification light, sound, + // and vibration. + .setDefaults(Notification.DEFAULT_ALL) + + // Set required fields, including the small icon, the + // notification title, and text. + .setSmallIcon(R.drawable.ic_stat_alarm) + .setContentTitle(title) + .setContentText(text) + + // All fields below this line are optional. + + // Use a default priority (recognized on devices running Android + // 4.1 or later) + .setPriority(NotificationCompat.PRIORITY_DEFAULT) + + // Provide a large icon, shown with the notification in the + // notification drawer on devices running Android 3.0 or later. + .setLargeIcon(picture) + + // Set ticker text (preview) information for this notification. + .setTicker(ticker) + + // Show a number. This is useful when stacking notifications of + // a single type. + .setNumber(number) + + // If this notification relates to a past or upcoming event, you + // should set the relevant time information using the setWhen + // method below. If this call is omitted, the notification's + // timestamp will by set to the time at which it was shown. + // TODO: Call setWhen if this notification relates to a past or + // upcoming event. The sole argument to this method should be + // the notification timestamp in milliseconds. + //.setWhen(...) + + // Set the pending intent to be initiated when the user touches + // the notification. + .setContentIntent( + PendingIntent.getActivity( + context, + 0, + new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")), + PendingIntent.FLAG_UPDATE_CURRENT)) + // Automatically dismiss the notification when it is touched. + .setAutoCancel(true); + + notify(context, builder.build()); + } + + @TargetApi(Build.VERSION_CODES.ECLAIR) + private static void notify(final Context context, final Notification notification) { + final NotificationManager nm = (NotificationManager) context + .getSystemService(Context.NOTIFICATION_SERVICE); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) { + nm.notify(NOTIFICATION_TAG, 0, notification); + } else { + nm.notify(NOTIFICATION_TAG.hashCode(), notification); + } + } + + /** + * Cancels any notifications of this type previously shown using + * {@link #notify(Context, String, int)}. + */ + @TargetApi(Build.VERSION_CODES.ECLAIR) + public static void cancel(final Context context) { + final NotificationManager nm = (NotificationManager) context + .getSystemService(Context.NOTIFICATION_SERVICE); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) { + nm.cancel(NOTIFICATION_TAG, 0); + } else { + nm.cancel(NOTIFICATION_TAG.hashCode()); + } + } +} diff --git a/android/FarmAlarm/app/src/main/java/com/zoblak/farmalarm/AppCompatPreferenceActivity.java b/android/FarmAlarm/app/src/main/java/com/zoblak/farmalarm/AppCompatPreferenceActivity.java new file mode 100644 index 0000000..4d150bd --- /dev/null +++ b/android/FarmAlarm/app/src/main/java/com/zoblak/farmalarm/AppCompatPreferenceActivity.java @@ -0,0 +1,109 @@ +package com.zoblak.farmalarm; + +import android.content.res.Configuration; +import android.os.Bundle; +import android.preference.PreferenceActivity; +import android.support.annotation.LayoutRes; +import android.support.annotation.Nullable; +import android.support.v7.app.ActionBar; +import android.support.v7.app.AppCompatDelegate; +import android.support.v7.widget.Toolbar; +import android.view.MenuInflater; +import android.view.View; +import android.view.ViewGroup; + +/** + * A {@link android.preference.PreferenceActivity} which implements and proxies the necessary calls + * to be used with AppCompat. + */ +public abstract class AppCompatPreferenceActivity extends PreferenceActivity { + + private AppCompatDelegate mDelegate; + + @Override + protected void onCreate(Bundle savedInstanceState) { + getDelegate().installViewFactory(); + getDelegate().onCreate(savedInstanceState); + super.onCreate(savedInstanceState); + } + + @Override + protected void onPostCreate(Bundle savedInstanceState) { + super.onPostCreate(savedInstanceState); + getDelegate().onPostCreate(savedInstanceState); + } + + public ActionBar getSupportActionBar() { + return getDelegate().getSupportActionBar(); + } + + public void setSupportActionBar(@Nullable Toolbar toolbar) { + getDelegate().setSupportActionBar(toolbar); + } + + @Override + public MenuInflater getMenuInflater() { + return getDelegate().getMenuInflater(); + } + + @Override + public void setContentView(@LayoutRes int layoutResID) { + getDelegate().setContentView(layoutResID); + } + + @Override + public void setContentView(View view) { + getDelegate().setContentView(view); + } + + @Override + public void setContentView(View view, ViewGroup.LayoutParams params) { + getDelegate().setContentView(view, params); + } + + @Override + public void addContentView(View view, ViewGroup.LayoutParams params) { + getDelegate().addContentView(view, params); + } + + @Override + protected void onPostResume() { + super.onPostResume(); + getDelegate().onPostResume(); + } + + @Override + protected void onTitleChanged(CharSequence title, int color) { + super.onTitleChanged(title, color); + getDelegate().setTitle(title); + } + + @Override + public void onConfigurationChanged(Configuration newConfig) { + super.onConfigurationChanged(newConfig); + getDelegate().onConfigurationChanged(newConfig); + } + + @Override + protected void onStop() { + super.onStop(); + getDelegate().onStop(); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + getDelegate().onDestroy(); + } + + public void invalidateOptionsMenu() { + getDelegate().invalidateOptionsMenu(); + } + + private AppCompatDelegate getDelegate() { + if (mDelegate == null) { + mDelegate = AppCompatDelegate.create(this, null); + } + return mDelegate; + } +} diff --git a/android/FarmAlarm/app/src/main/java/com/zoblak/farmalarm/PeriodicalPingReceiver.java b/android/FarmAlarm/app/src/main/java/com/zoblak/farmalarm/PeriodicalPingReceiver.java index 8cd1afe..64e82d9 100644 --- a/android/FarmAlarm/app/src/main/java/com/zoblak/farmalarm/PeriodicalPingReceiver.java +++ b/android/FarmAlarm/app/src/main/java/com/zoblak/farmalarm/PeriodicalPingReceiver.java @@ -3,6 +3,8 @@ package com.zoblak.farmalarm; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; public class PeriodicalPingReceiver extends BroadcastReceiver { public PeriodicalPingReceiver() { @@ -10,10 +12,13 @@ public class PeriodicalPingReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { - // TODO: This method is called when the BroadcastReceiver is receiving - // an Intent broadcast. - throw new UnsupportedOperationException("Not yet implemented"); - AlarmPollingService.startActionPing(context, ); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + String controllers = prefs.getString("controllers", null); + Boolean isAlarmOn = prefs.getBoolean("alarm_set", false); + + if(isAlarmOn && controllers != null) { + AlarmPollingService.startActionPing(context, controllers); + } } } diff --git a/android/FarmAlarm/app/src/main/java/com/zoblak/farmalarm/SettingsActivity.java b/android/FarmAlarm/app/src/main/java/com/zoblak/farmalarm/SettingsActivity.java new file mode 100644 index 0000000..a65d68e --- /dev/null +++ b/android/FarmAlarm/app/src/main/java/com/zoblak/farmalarm/SettingsActivity.java @@ -0,0 +1,208 @@ +package com.zoblak.farmalarm; + + +import android.annotation.TargetApi; +import android.content.Context; +import android.content.Intent; +import android.content.res.Configuration; +import android.media.Ringtone; +import android.media.RingtoneManager; +import android.net.Uri; +import android.os.Build; +import android.os.Bundle; +import android.preference.ListPreference; +import android.preference.Preference; +import android.preference.PreferenceActivity; +import android.support.v7.app.ActionBar; +import android.preference.PreferenceFragment; +import android.preference.PreferenceManager; +import android.preference.RingtonePreference; +import android.text.TextUtils; +import android.view.MenuItem; +import android.support.v4.app.NavUtils; + +import java.util.List; + +/** + * A {@link PreferenceActivity} that presents a set of application settings. On + * handset devices, settings are presented as a single list. On tablets, + * settings are split by category, with category headers shown to the left of + * the list of settings. + *
+ * See
+ * Android Design: Settings for design guidelines and the Settings
+ * API Guide for more information on developing a Settings UI.
+ */
+public class SettingsActivity extends AppCompatPreferenceActivity {
+ /**
+ * A preference value change listener that updates the preference's summary
+ * to reflect its new value.
+ */
+ private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
+ @Override
+ public boolean onPreferenceChange(Preference preference, Object value) {
+ String stringValue = value.toString();
+
+ if (preference instanceof ListPreference) {
+ // For list preferences, look up the correct display value in
+ // the preference's 'entries' list.
+ ListPreference listPreference = (ListPreference) preference;
+ int index = listPreference.findIndexOfValue(stringValue);
+
+ // Set the summary to reflect the new value.
+ preference.setSummary(
+ index >= 0
+ ? listPreference.getEntries()[index]
+ : null);
+
+ } else if (preference instanceof RingtonePreference) {
+ // For ringtone preferences, look up the correct display value
+ // using RingtoneManager.
+ if (TextUtils.isEmpty(stringValue)) {
+ // Empty values correspond to 'silent' (no ringtone).
+ //preference.setSummary(R.string.pref_ringtone_silent);
+
+ } else {
+ Ringtone ringtone = RingtoneManager.getRingtone(
+ preference.getContext(), Uri.parse(stringValue));
+
+ if (ringtone == null) {
+ // Clear the summary if there was a lookup error.
+ preference.setSummary(null);
+ } else {
+ // Set the summary to reflect the new ringtone display
+ // name.
+ String name = ringtone.getTitle(preference.getContext());
+ preference.setSummary(name);
+ }
+ }
+
+ } else {
+ // For all other preferences, set the summary to the value's
+ // simple string representation.
+ preference.setSummary(stringValue);
+ }
+ return true;
+ }
+ };
+
+ /**
+ * Helper method to determine if the device has an extra-large screen. For
+ * example, 10" tablets are extra-large.
+ */
+ private static boolean isXLargeTablet(Context context) {
+ return (context.getResources().getConfiguration().screenLayout
+ & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
+ }
+
+ /**
+ * Binds a preference's summary to its value. More specifically, when the
+ * preference's value is changed, its summary (line of text below the
+ * preference title) is updated to reflect the value. The summary is also
+ * immediately updated upon calling this method. The exact display format is
+ * dependent on the type of preference.
+ *
+ * @see #sBindPreferenceSummaryToValueListener
+ */
+ private static void bindPreferenceSummaryToValue(Preference preference) {
+ // Set the listener to watch for value changes.
+ preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
+
+ // Trigger the listener immediately with the preference's
+ // current value.
+ sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
+ PreferenceManager
+ .getDefaultSharedPreferences(preference.getContext())
+ .getString(preference.getKey(), ""));
+ }
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setupActionBar();
+ }
+
+ /**
+ * Set up the {@link android.app.ActionBar}, if the API is available.
+ */
+ private void setupActionBar() {
+ ActionBar actionBar = getSupportActionBar();
+ if (actionBar != null) {
+ // Show the Up button in the action bar.
+ actionBar.setDisplayHomeAsUpEnabled(true);
+ }
+ }
+
+ @Override
+ public boolean onMenuItemSelected(int featureId, MenuItem item) {
+ int id = item.getItemId();
+ if (id == android.R.id.home) {
+ if (!super.onMenuItemSelected(featureId, item)) {
+ NavUtils.navigateUpFromSameTask(this);
+ }
+ return true;
+ }
+ return super.onMenuItemSelected(featureId, item);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean onIsMultiPane() {
+ return isXLargeTablet(this);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ @TargetApi(Build.VERSION_CODES.HONEYCOMB)
+ public void onBuildHeaders(List