summaryrefslogtreecommitdiffstats
path: root/fmapp2/src/com/caf/fmradio/StationListActivity.java
blob: 46b59ad59f55a3036e612423bdf81a4d2495ffaa (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
 * Copyright (C) 2012-2013, The Linux Foundation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 *       copyright notice, this list of conditions and the following
 *       disclaimer in the documentation and/or other materials provided
 *       with the distribution.
 *     * Neither the name of The Linux Foundation nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.caf.fmradio;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.Handler.Callback;
import android.text.TextUtils;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

public class StationListActivity extends Activity implements
        View.OnCreateContextMenuListener {

    private static final String LOGTAG = "StationList";
    private ListView mStationList;
    private PresetList mPresetList;
    private SimpleAdapter mAdapter;
    private static IFMRadioService mService = null;
    private static final int CONTEXT_MENU_RENAME = 1;
    private static final int CONTEXT_MENU_DELETE = 2;

    static final int DIALOG_RENAME_ID = 0;
    static final int DIALOG_DELETE_ID = 1;
    // private static String mDialogTitle;
    private static String mDialogStationName;
    private static int mItemId;
    private Dialog mRenameDialog;
    private Dialog mDeleteDialog;
    //whether is the first headset plug event
    private boolean isFirst = true;
    private ServiceConnection osc = new ServiceConnection() {
        public void onServiceConnected(ComponentName classname, IBinder obj) {
            mService = IFMRadioService.Stub.asInterface(obj);
            if (mService != null) {
                return;
            } else {
                Log.e(LOGTAG, "IFMRadioService onServiceConnected failed");
            }
        }

        public void onServiceDisconnected(ComponentName classname) {
        }
    };

    ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
    HashMap<Integer, Integer> mIndex = new HashMap<Integer, Integer>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.station_list);
        bindService((new Intent()).setClass(this, FMRadioService.class), osc, 0);

        mStationList = (ListView) findViewById(R.id.station_list);
        // mPresetList = new PresetList("StationList");
        mStationList.setOnCreateContextMenuListener(this);
        mStationList.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                String freq = ((HashMap<String, String>) mAdapter.getItem(arg2))
                        .get("freq");
                Float fFreq = Float.parseFloat(freq);
                if (mService != null) {
                    try {
                        mService.tune((int) ((fFreq * 1000)));
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                } else {
                    Log.d(LOGTAG, "mService is null........");
                }
            }
        });
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        AdapterContextMenuInfo mi = (AdapterContextMenuInfo) item.getMenuInfo();
        switch (item.getItemId()) {
        case CONTEXT_MENU_RENAME:
            showDialog(DIALOG_RENAME_ID);
            break;
        case CONTEXT_MENU_DELETE:
            showDialog(DIALOG_DELETE_ID);
            break;

        }

        return true;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        // TODO Auto-generated method stub
        AdapterContextMenuInfo mi = (AdapterContextMenuInfo) menuInfo;
        menu.add(0, CONTEXT_MENU_RENAME, 0, getString(R.string.preset_rename));
        menu.add(0, CONTEXT_MENU_DELETE, 0, getString(R.string.preset_delete));
        mItemId = mi.position;
        menu.setHeaderTitle(getString(R.string.station_name)+getNameFromId(mItemId));
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        load();
        super.onResume();
    }

    private boolean stationNameExist(String name) {
        for (HashMap<String, String> item : list) {
            if (item != null && name.equals(item.get("name"))) {
                return true;
            }
        }
        return false;
    }

    @Override
    protected void onPrepareDialog(int id, Dialog dialog, Bundle b) {
        // TODO Auto-generated method stub
        // super.onPrepareDialog(id, dialog);
        // After change system language, current function will be executed before
        // onResume , so execute load to ensure adapter is not null.
        load();
        switch (id) {
        case DIALOG_RENAME_ID:
            mRenameDialog.setTitle(getString(R.string.station_name)+getNameFromId(mItemId));
            final EditText editText = (EditText) mRenameDialog
                    .findViewById(R.id.name);
            editText.setText(getNameFromId(mItemId));
            Button bOk = (Button) mRenameDialog.findViewById(R.id.save);

            bOk.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String rename = editText.getText().toString();
                    if (TextUtils.isEmpty(rename) || TextUtils.isEmpty(rename.trim())) {
                        Context context = getApplicationContext();
                        Toast toast = Toast.makeText(context, getString(R.string.station_name_empty),
                                Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);
                        toast.show();
                    } else if (stationNameExist(rename)) {
                        Context context = getApplicationContext();
                        Toast toast = Toast.makeText(context,
                                getString(R.string.station_name_exist, rename), Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);
                        toast.show();
                    } else {
                        saveStationName(mItemId,rename);
                        mRenameDialog.dismiss();
                    }
                }

            });
            Button bCancel = (Button) mRenameDialog.findViewById(R.id.cancel);
            bCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    mRenameDialog.dismiss();
                }
            });
            break;
        case DIALOG_DELETE_ID:
            mDeleteDialog.setTitle(getString(R.string.station_list_delete_station, getNameFromId(mItemId)));
            TextView prompt = (TextView) mDeleteDialog.findViewById(R.id.prompt);
            prompt.setText(getString(R.string.station_list_delete_station_prompt,getNameFromId(mItemId)));
            Button bDelete = (Button) mDeleteDialog.findViewById(R.id.delete);

            bDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    deleteStation(mItemId);
                    mDeleteDialog.dismiss();
                }
            });
            Button bCancelDelete = (Button) mDeleteDialog.findViewById(R.id.cancel);
            bCancelDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mDeleteDialog.dismiss();
                }
            });
            break;
        }
    }

    @Override
    protected Dialog onCreateDialog(int id, Bundle b) {
        // TODO Auto-generated method stub
        //should return different dialog
        switch (id) {
        case DIALOG_RENAME_ID:
            mRenameDialog = new Dialog(this);
            mRenameDialog.setContentView(R.layout.rename_dialog);
            return mRenameDialog;
        case DIALOG_DELETE_ID:
            mDeleteDialog = new Dialog(this);
            mDeleteDialog.setContentView(R.layout.delete_dialog);
            return mDeleteDialog;
        default:
            ;
        }
        return null;
    }

    private void saveStationName(int id, String name) {
        Integer stationIndex = mIndex.get(new Integer(id));
        SharedPreferences sp = getSharedPreferences(
                FMRadio.SCAN_STATION_PREFS_NAME, 0);
        SharedPreferences.Editor editor = sp.edit();
        editor
                .putString(FMRadio.STATION_NAME + (stationIndex.intValue()),
                        name);
        editor.commit();
        load();
    }

    private void deleteStation(int id) {
        SharedPreferences sp = getSharedPreferences(
                FMRadio.SCAN_STATION_PREFS_NAME, 0);
        Integer stationIndex = mIndex.get(new Integer(id));
        SharedPreferences.Editor editor = sp.edit();
        editor.remove(FMRadio.STATION_NAME + (stationIndex));
        editor.remove(FMRadio.STATION_FREQUENCY + (stationIndex));
        editor.commit();
        load();
    }

    private int getFrequencyFromId(int id) {
        String freq = ((HashMap<String, String>) mAdapter.getItem(id))
                .get("freq");
        Float fFreq = Float.parseFloat(freq);
        return (int) ((fFreq * 1000));
    }

    private String getNameFromId(int id) {
        String name = ((HashMap<String, String>) mAdapter.getItem(id))
                .get("name");
        return name;

    }

    protected void load() {
        list.clear();
        mIndex.clear();
        SharedPreferences sp = getSharedPreferences(
                FMRadio.SCAN_STATION_PREFS_NAME, 0);
        int station_number = sp.getInt(FMRadio.NUM_OF_STATIONS, 0);
        for (int i = 1; i <= station_number; i++) {
            HashMap<String, String> item = new HashMap<String, String>();
            String name = sp.getString(FMRadio.STATION_NAME + i, "");

            int frequency = sp.getInt(FMRadio.STATION_FREQUENCY + i, 0);

            if (!name.equals("") && frequency != 0) {
                item.put("name", name);
                item.put("freq", String.valueOf(frequency / 1000.0f));
                mIndex.put(new Integer(list.size()), new Integer(i));
                list.add(item);
            }
        }

        mAdapter = new SimpleAdapter(this, list, R.layout.station_list_item,
                new String[] { "name", "freq" }, new int[] { R.id.name,
                        R.id.freq });

        mStationList.setAdapter(mAdapter);
    }

    @Override
    protected void onDestroy() {
        unbindService(osc);
        mService = null;
        super.onDestroy();
    }

}