summaryrefslogtreecommitdiffstats
path: root/android/src/org/rockbox/widgets/RockboxWidgetConfigure.java
blob: 6ce20507805facc3737a8b9bc56b16b96e6fde4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2011 Antoine Cellerier <dionoea at videolan dot org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

package org.rockbox.widgets;

import org.rockbox.R;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;


public class RockboxWidgetConfigure extends Activity
{
    int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;

    public RockboxWidgetConfigure()
    {
        super();
    }

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

        setResult(RESULT_CANCELED);
        setContentView(R.layout.appwidget_configure);

        ((CheckBox)findViewById(R.id.enable_prev)).setChecked(false);
        ((CheckBox)findViewById(R.id.enable_stop)).setChecked(true);
        ((CheckBox)findViewById(R.id.enable_playpause)).setChecked(true);
        ((CheckBox)findViewById(R.id.enable_next)).setChecked(false);

        findViewById(R.id.confirm).setOnClickListener(mCreateWidget);

        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (extras != null)
            mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);

        if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID)
            finish();
    }

    View.OnClickListener mCreateWidget = new View.OnClickListener()
    {
        public void onClick(View v)
        {
            final Context context = RockboxWidgetConfigure.this;

            WidgetPref state = new WidgetPref();
            state.enablePrev = ((CheckBox)findViewById(R.id.enable_prev)).isChecked();
            state.enableStop = ((CheckBox)findViewById(R.id.enable_stop)).isChecked();
            state.enablePlayPause = ((CheckBox)findViewById(R.id.enable_playpause)).isChecked();
            state.enableNext = ((CheckBox)findViewById(R.id.enable_next)).isChecked();
            saveWidgetPref(context, mAppWidgetId, state);

            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
            RockboxWidgetProvider.getInstance().updateAppWidget(context, appWidgetManager, mAppWidgetId, null);

            Intent result = new Intent();
            result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
            setResult(RESULT_OK, result);
            finish();
        }
    };

    static public class WidgetPref
    {
        public boolean enablePrev = true;
        public boolean enableStop = true;
        public boolean enablePlayPause = true;
        public boolean enableNext = true;
    }

    static void saveWidgetPref(Context context, int appWidgetId, WidgetPref state)
    {
        SharedPreferences.Editor prefs = context.getSharedPreferences("org.rockbox.RockboxWidgetConfigure", 0).edit();
        prefs.putBoolean("prev"+appWidgetId, state.enablePrev);
        prefs.putBoolean("stop"+appWidgetId, state.enableStop);
        prefs.putBoolean("playpause"+appWidgetId, state.enablePlayPause);
        prefs.putBoolean("next"+appWidgetId, state.enableNext);
        prefs.commit();
    }

    static WidgetPref loadWidgetPref(Context context, int appWidgetId)
    {
        SharedPreferences prefs = context.getSharedPreferences("org.rockbox.RockboxWidgetConfigure", 0);
        WidgetPref state = new WidgetPref();
        state.enablePrev = prefs.getBoolean("prev"+appWidgetId, true);
        state.enableStop = prefs.getBoolean("stop"+appWidgetId, true);
        state.enablePlayPause = prefs.getBoolean("playpause"+appWidgetId, true);
        state.enableNext = prefs.getBoolean("next"+appWidgetId, true);
        return state;
    }
}