Skip to content
Snippets Groups Projects
Commit 717be180 authored by void's avatar void
Browse files

Merge branch 'master' of warpzone.ms:wz-status

parents a7b509a3 18ad7e6c
No related branches found
No related tags found
No related merge requests found
Showing
with 692 additions and 0 deletions
<resources>
<!--
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
<!-- API 11 theme customizations can go here. -->
</style>
</resources>
\ No newline at end of file
<resources>
<!--
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- API 14 theme customizations can go here. -->
</style>
</resources>
\ No newline at end of file
<resources>
<string name="app_name">Warpzone Status Widget</string>
</resources>
\ No newline at end of file
<resources>
<!-- Strings related to Settings -->
<!-- Example settings for Data & Sync -->
<string name="pref_header_data_sync">Data &amp; sync</string>
<string name="pref_title_sync_frequency">Sync frequency</string>
<string-array name="pref_sync_frequency_titles">
<item>5 minutes</item>
<item>10 minutes</item>
<item>15 minutes</item>
<item>30 minutes</item>
<item>1 hour</item>
<item>3 hours</item>
<item>6 hours</item>
<item>Never</item>
</string-array>
<string-array name="pref_sync_frequency_values">
<item>5</item>
<item>10</item>
<item>15</item>
<item>30</item>
<item>60</item>
<item>180</item>
<item>360</item>
<item>-1</item>
</string-array>
<string name="pref_title_system_sync_settings">System sync settings</string>
<!-- Example settings for Notifications -->
<string name="pref_header_notifications">Notifications</string>
<string name="pref_title_status_change_notifications">Status change notifications</string>
<string name="pref_title_ringtone">Ringtone</string>
<string name="pref_ringtone_silent">Silent</string>
<string name="pref_title_vibrate">Vibrate</string>
<string name="pref_button_done">Done</string>
</resources>
\ No newline at end of file
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</resources>
\ No newline at end of file
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<!--
NOTE: Hide buttons to simplify the UI. Users can touch outside the dialog to
dismiss it.
-->
<!-- NOTE: ListPreference's summary should be set to its value by the activity code. -->
<ListPreference
android:defaultValue="15"
android:entries="@array/pref_sync_frequency_titles"
android:entryValues="@array/pref_sync_frequency_values"
android:key="sync_frequency"
android:negativeButtonText="@null"
android:positiveButtonText="@null"
android:title="@string/pref_title_sync_frequency" />
<!--
This preference simply launches an intent when selected. Use this UI sparingly, per
design guidelines.
-->
<Preference android:title="@string/pref_title_system_sync_settings" >
<intent android:action="android.settings.SYNC_SETTINGS" />
</Preference>
</PreferenceScreen>
\ No newline at end of file
<preference-headers xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- These settings headers are only used on tablets. -->
<header
android:fragment="${packageName}.${activityClass}$DataSyncPreferenceFragment"
android:title="@string/pref_header_data_sync" />
<header
android:fragment="${packageName}.${activityClass}$NotificationPreferenceFragment"
android:title="@string/pref_header_notifications" />
</preference-headers>
\ No newline at end of file
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<!--
A 'parent' preference, which enables/disables child preferences (below)
when checked/unchecked.
-->
<CheckBoxPreference
android:defaultValue="true"
android:key="notifications_status_change"
android:title="@string/pref_title_status_change_notifications" />
<!-- Allows the user to choose a ringtone in the 'notification' category. -->
<!-- NOTE: This preference will be enabled only when the checkbox above is checked. -->
<!-- NOTE: RingtonePreference's summary should be set to its value by the activity code. -->
<RingtonePreference
android:defaultValue="content://settings/system/notification_sound"
android:dependency="notifications_status_change"
android:key="notifications_status_change_ringtone"
android:ringtoneType="notification"
android:title="@string/pref_title_ringtone" />
<!-- NOTE: This preference will be enabled only when the checkbox above is checked. -->
<CheckBoxPreference
android:defaultValue="true"
android:dependency="notifications_status_change"
android:key="notifications_status_change_vibrate"
android:title="@string/pref_title_vibrate" />
</PreferenceScreen>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minHeight="64dp"
android:minWidth="64dp"
android:configure="ms.warpzone.warpzonestatuswidget.SettingsActivity"
android:updatePeriodMillis="3600000">
</appwidget-provider>
\ No newline at end of file
package ms.warpzone.warpzonestatuswidget;
import android.annotation.TargetApi;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
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.preference.PreferenceCategory;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.preference.RingtonePreference;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
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.
* <p>
* See <a href="http://developer.android.com/design/patterns/settings.html">
* Android Design: Settings</a> for design guidelines and the <a
* href="http://developer.android.com/guide/topics/ui/settings.html">Settings
* API Guide</a> for more information on developing a Settings UI.
*/
public class SettingsActivity extends PreferenceActivity implements OnClickListener {
/**
* Determines whether to always show the simplified settings UI, where
* settings are presented in a single list. When false, settings are shown
* as a master/detail two-pane view on tablets. When true, a single pane is
* shown on tablets.
*/
private static final boolean ALWAYS_SIMPLE_PREFS = false;
private int widgetID;
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
setupSimplePreferencesScreen();
widgetID = AppWidgetManager.INVALID_APPWIDGET_ID;
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null)
widgetID = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
if (widgetID == AppWidgetManager.INVALID_APPWIDGET_ID)
finish();
setResult(RESULT_CANCELED);
ListView v = getListView();
Button b = new Button(this);
b.setOnClickListener(this);
b.setText(R.string.pref_button_done);
v.addFooterView(b);
}
public void onClick(View view) {
Log.d("Done", "First onUpdate broadcast sending...");
final Context context = SettingsActivity.this;
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName thisAppWidget = new ComponentName(context.getPackageName(), SettingsActivity.class.getName());
Intent firstUpdate = new Intent(context, WidgetProvider.class);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
firstUpdate.setAction("android.appwidget.action.APPWIDGET_UPDATE");
firstUpdate.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
context.sendBroadcast(firstUpdate);
Log.d("Done", "First onUpdate broadcast sent");
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetID);
setResult(RESULT_OK, resultValue);
finish();
}
/**
* Shows the simplified settings UI if the device configuration if the
* device configuration dictates that a simplified, single-pane UI should be
* shown.
*/
private void setupSimplePreferencesScreen() {
if (!isSimplePreferences(this)) {
return;
}
// Add 'general' preferences.
addPreferencesFromResource(R.xml.pref_data_sync);
// Add 'notifications' preferences, and a corresponding header.
PreferenceCategory fakeHeader = new PreferenceCategory(this);
fakeHeader.setTitle(R.string.pref_header_notifications);
getPreferenceScreen().addPreference(fakeHeader);
addPreferencesFromResource(R.xml.pref_notification);
// Bind the summaries of EditText/List/Dialog/Ringtone preferences to
// their values. When their values change, their summaries are updated
// to reflect the new value, per the Android Design guidelines.
bindPreferenceSummaryToValue(findPreference("notifications_status_change_ringtone"));
bindPreferenceSummaryToValue(findPreference("sync_frequency"));
}
/** {@inheritDoc} */
@Override
public boolean onIsMultiPane() {
return isXLargeTablet(this) && !isSimplePreferences(this);
}
/**
* 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;
}
/**
* Determines whether the simplified settings UI should be shown. This is
* true if this is forced via {@link #ALWAYS_SIMPLE_PREFS}, or the device
* doesn't have newer APIs like {@link PreferenceFragment}, or the device
* doesn't have an extra-large screen. In these cases, a single-pane
* "simplified" settings UI should be shown.
*/
private static boolean isSimplePreferences(Context context) {
return ALWAYS_SIMPLE_PREFS
|| Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB
|| !isXLargeTablet(context);
}
/** {@inheritDoc} */
@Override
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void onBuildHeaders(List<Header> target) {
if (!isSimplePreferences(this)) {
loadHeadersFromResource(R.xml.pref_headers, target);
}
}
/**
* 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;
}
};
/**
* 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(), ""));
}
/**
* This fragment shows notification preferences only. It is used when the
* activity is showing a two-pane settings UI.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class NotificationPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_notification);
bindPreferenceSummaryToValue(findPreference("notifications_status_change_ringtone"));
}
}
/**
* This fragment shows data and sync preferences only. It is used when the
* activity is showing a two-pane settings UI.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class DataSyncPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_data_sync);
bindPreferenceSummaryToValue(findPreference("sync_frequency"));
}
}
}
package ms.warpzone.warpzonestatuswidget;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONException;
import org.json.JSONObject;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.StrictMode;
import android.text.format.DateFormat;
import android.util.Log;
public class StatusLoader {
private static String loadJSONPage(String url) {
try {
URL uri = new URL(url);
HttpURLConnection connection = (HttpURLConnection) uri.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String result = "";
String line = reader.readLine();
if(!line.contains("{"))
{
Log.d("loadJSONPage", "no JSON found on page.");
return null;
}
do {
result += line + "\n";
} while ((line = reader.readLine()) != null);
return result;
} catch (Exception e) {
if(e != null)
Log.d("loadJSONPage", e.getMessage());
return null;
}
}
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
private static void allowNetworkingInMainThread() {
//Allow Networking in this thread.
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
public static int getStatusImageID() {
try {
allowNetworkingInMainThread();
}
catch(Exception e)
{
Log.d("getStatusImageID", "allowNetworkingInMainThread not supported.");
}
String content = loadJSONPage("http://warpzone.ms/api/status.php");
if(content == null) {
Log.d("getStatusImageID", "Could not load status page.");
return R.drawable.warpzone_logo_small;
}
JSONObject json;
try {
json = new JSONObject(content);
long record_time = json.getLong("timestamp");
long now = System.currentTimeMillis()/1000L;
if((now - record_time) > 600)
{
Log.d("getStatusImageID", "Status record is too old.");
return R.drawable.warpzone_logo_orange_small;
}
if(json.getInt("tuerOffen") != 0)
{
return R.drawable.warpzone_logo_green_small;
}
return R.drawable.warpzone_logo_red_small;
} catch (JSONException e) {
Log.d("getStatusImageID", "Could not parse status JSON.");
return R.drawable.warpzone_logo_orange_small;
}
}
}
\ No newline at end of file
package ms.warpzone.warpzonestatuswidget;
import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;
public class UpdateWidgetService extends Service {
@Override
public void onStart(Intent intent, int startId) {
Log.d("onStart", "Called");
// Create some random data
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext());
int[] allWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
ComponentName thisWidget = new ComponentName(getApplicationContext(), WidgetProvider.class);
int[] allWidgetIds2 = appWidgetManager.getAppWidgetIds(thisWidget);
Log.d("onStart", "From Intent" + String.valueOf(allWidgetIds.length));
Log.d("onStart", "Direct" + String.valueOf(allWidgetIds2.length));
for (int widgetId : allWidgetIds) {
RemoteViews remoteViews = new RemoteViews(this.getApplicationContext().getPackageName(), R.layout.widget_layout);
// Set the text
remoteViews.setTextViewText(R.id.update, "Random: " + String.valueOf(number));
// Register an onClickListener
Intent clickIntent = new Intent(this.getApplicationContext(), WidgetProvider.class);
clickIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.update, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
stopSelf();
super.onStart(intent, startId);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
\ No newline at end of file
package ms.warpzone.warpzonestatuswidget;
import java.util.Random;
import android.annotation.TargetApi;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.os.StrictMode;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.RemoteViews;
public class WidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Log.d("onUpdate", "called");
// get the widget views
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
// loading animation
remoteViews.setImageViewResource(R.id.imageView, android.R.drawable.stat_notify_sync);//ic_popup_sync);
for (int widgetId : appWidgetIds)
sendUpdate(context, appWidgetManager, widgetId, remoteViews);
// load the settings
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
// keep the refresh timer running
updateAlarm(context, settings, appWidgetIds);
// load status and send notification, if appropriate
int status = StatusLoader.getStatusImageID();
if(statusChanged(context, settings, status) && settings.getBoolean("notifications_status_change", false))
sendNotification(context, settings);
// set new status image
remoteViews.setImageViewResource(R.id.imageView, status);
for (int widgetId : appWidgetIds)
sendUpdate(context, appWidgetManager, widgetId, remoteViews);
}
private void updateAlarm(Context context, SharedPreferences settings, int[] appWidgetIds) {
Intent intent = get_ACTION_APPWIDGET_UPDATE_Intent(context);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, appWidgetIds[0], intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pendingIntent);
int minutes = Integer.parseInt(settings.getString("sync_frequency", "15"));
if(minutes > 0)
{
long interval = 1000*60*minutes;
alarm.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + interval, interval, pendingIntent);
}
}
private void sendNotification(Context context, SharedPreferences settings) {
int icon = settings.getInt("status", -1);
Uri sound = Uri.parse(settings.getString("notifications_status_change_ringtone", "content://settings/system/notification_sound"));
boolean vibrate = settings.getBoolean("notifications_status_change_vibrate", false);
String title = "Warpzone Status";
String message = "Der Warpzone-Status hat sich geändert!";
Intent notificationIntent = new Intent(context, WidgetProvider.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
Notification notification = new Notification(icon, message, System.currentTimeMillis());
notification.sound = sound;
notification.flags = Notification.FLAG_AUTO_CANCEL;
if(vibrate)
notification.defaults = Notification.DEFAULT_VIBRATE;
notification.setLatestEventInfo(context, title, message, contentIntent);
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(new Random().nextInt(), notification);
}
private boolean statusChanged(Context context, SharedPreferences settings, int status_new)
{
if(status_new != R.drawable.warpzone_logo_small && settings.getInt("status", -1) != status_new)
{
SharedPreferences.Editor editor = settings.edit();
editor.putInt("status", status_new);
editor.commit();
return true;
}
return false;
}
private void sendUpdate(Context context, AppWidgetManager appWidgetManager, int appWidgetId, RemoteViews remoteViews)
{
//When user click on the label, update ALL the instances of the widget.
Intent labelIntent = get_ACTION_APPWIDGET_UPDATE_Intent(context);
PendingIntent labelPendingIntent = PendingIntent.getBroadcast(context, appWidgetId, labelIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.imageView, labelPendingIntent);
Log.d("updateAppWidget", "Updated ID: " + appWidgetId);
//Call the Manager to ensure the changes take effect.
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
/**
* Utility method to ensure that when we want an Intent that fire ACTION_APPWIDGET_UPDATE, the extras are correct.<br>
* The default implementation of onReceive() will discard it if we don't add the ids of all the instances.
* @param context
* @return
*/
protected Intent get_ACTION_APPWIDGET_UPDATE_Intent(Context context) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName thisAppWidget = new ComponentName(context.getPackageName(), WidgetProvider.class.getName());
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
Intent intent = new Intent(context, WidgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
return intent;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment