Android: Simplify media button intent generation in the widget and cleanup RockboxSer...
[maemo-rb.git] / android / src / org / rockbox / widgets / RockboxWidgetConfigure.java
blob6ce20507805facc3737a8b9bc56b16b96e6fde4a
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2011 Antoine Cellerier <dionoea at videolan dot org>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 package org.rockbox.widgets;
24 import org.rockbox.R;
26 import android.app.Activity;
27 import android.appwidget.AppWidgetManager;
28 import android.content.Context;
29 import android.content.Intent;
30 import android.content.SharedPreferences;
31 import android.os.Bundle;
32 import android.view.View;
33 import android.widget.CheckBox;
36 public class RockboxWidgetConfigure extends Activity
38 int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
40 public RockboxWidgetConfigure()
42 super();
45 @Override
46 public void onCreate(Bundle savedInstanceState)
48 super.onCreate(savedInstanceState);
50 setResult(RESULT_CANCELED);
51 setContentView(R.layout.appwidget_configure);
53 ((CheckBox)findViewById(R.id.enable_prev)).setChecked(false);
54 ((CheckBox)findViewById(R.id.enable_stop)).setChecked(true);
55 ((CheckBox)findViewById(R.id.enable_playpause)).setChecked(true);
56 ((CheckBox)findViewById(R.id.enable_next)).setChecked(false);
58 findViewById(R.id.confirm).setOnClickListener(mCreateWidget);
60 Intent intent = getIntent();
61 Bundle extras = intent.getExtras();
62 if (extras != null)
63 mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
65 if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID)
66 finish();
69 View.OnClickListener mCreateWidget = new View.OnClickListener()
71 public void onClick(View v)
73 final Context context = RockboxWidgetConfigure.this;
75 WidgetPref state = new WidgetPref();
76 state.enablePrev = ((CheckBox)findViewById(R.id.enable_prev)).isChecked();
77 state.enableStop = ((CheckBox)findViewById(R.id.enable_stop)).isChecked();
78 state.enablePlayPause = ((CheckBox)findViewById(R.id.enable_playpause)).isChecked();
79 state.enableNext = ((CheckBox)findViewById(R.id.enable_next)).isChecked();
80 saveWidgetPref(context, mAppWidgetId, state);
82 AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
83 RockboxWidgetProvider.getInstance().updateAppWidget(context, appWidgetManager, mAppWidgetId, null);
85 Intent result = new Intent();
86 result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
87 setResult(RESULT_OK, result);
88 finish();
92 static public class WidgetPref
94 public boolean enablePrev = true;
95 public boolean enableStop = true;
96 public boolean enablePlayPause = true;
97 public boolean enableNext = true;
100 static void saveWidgetPref(Context context, int appWidgetId, WidgetPref state)
102 SharedPreferences.Editor prefs = context.getSharedPreferences("org.rockbox.RockboxWidgetConfigure", 0).edit();
103 prefs.putBoolean("prev"+appWidgetId, state.enablePrev);
104 prefs.putBoolean("stop"+appWidgetId, state.enableStop);
105 prefs.putBoolean("playpause"+appWidgetId, state.enablePlayPause);
106 prefs.putBoolean("next"+appWidgetId, state.enableNext);
107 prefs.commit();
110 static WidgetPref loadWidgetPref(Context context, int appWidgetId)
112 SharedPreferences prefs = context.getSharedPreferences("org.rockbox.RockboxWidgetConfigure", 0);
113 WidgetPref state = new WidgetPref();
114 state.enablePrev = prefs.getBoolean("prev"+appWidgetId, true);
115 state.enableStop = prefs.getBoolean("stop"+appWidgetId, true);
116 state.enablePlayPause = prefs.getBoolean("playpause"+appWidgetId, true);
117 state.enableNext = prefs.getBoolean("next"+appWidgetId, true);
118 return state;