/*
 * ServeStream: A HTTP stream browser/player for Android
 * Copyright 2010 William Seemann
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.sourceforge.servestream.alarm;

import java.util.ArrayList;

import net.sourceforge.servestream.dbutils.Stream;
import net.sourceforge.servestream.dbutils.StreamDatabase;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.preference.ListPreference;
import android.util.AttributeSet;

/**
 * The RingtonePreference does not have a way to get/set the current ringtone so
 * we override onSaveRingtone and onRestoreRingtone to get the same behavior.
 */
public class AlarmPreference extends ListPreference {

	private int [] mIds;
	private String [] mNicknames;
	
    // Initial value that can be set with the values saved in the database.
    private int mId = 0;
    // New value that will be set if a positive result comes back from the
    // dialog.
    private int mNewId = -1;
	
	protected StreamDatabase mStreamdb = null;
    
	public AlarmPreference(Context context, AttributeSet attrs) {
		super(context, attrs);
        
		// connect with streams database and populate list
		mStreamdb = new StreamDatabase(getContext());
		
		ArrayList<Stream> streams = mStreamdb.getStreams();

		mStreamdb.close();
		
		String [] entries = new String[(streams.size() + 1)];
		String [] entryValues = new String[(streams.size() + 1)];
		mIds = new int[streams.size() + 1];
		mNicknames = new String[streams.size() + 1];
		
		entries[0] = "Silent";
		entryValues[0] = String.valueOf("0");
		mIds[0] = 0;
		mNicknames[0] = "Silent";
		
		for (int i = 0; i < streams.size(); i++) {
			entries[i + 1] = streams.get(i).getNickname();
			entryValues[i + 1] = String.valueOf(i + 1);
			mIds[i + 1] = (int) streams.get(i).getId();
			mNicknames[i + 1] = streams.get(i).getNickname();
		}
		
        setEntries(entries);
        setEntryValues(entryValues);
	}
 
    @Override
    protected void onDialogClosed(boolean positiveResult) {
        if (positiveResult) {
            mId = mNewId;
            setSummary(mNicknames[mId]);
            callChangeListener(mId);
        }
    }
	
	@Override
	protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {	
        CharSequence[] entries = getEntries();
        
        builder.setSingleChoiceItems(
        		entries,
        		mId,
                new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						mNewId = which;
					}
				});
	}

	public void setAlertId(int id) {
		for (int i = 0; i < mIds.length; i++) {
			if (mIds[i] == id) {
				mId = i;
				mNewId = i;
	            setSummary(mNicknames[mId]);
			}
		}
	}
	
    public int getAlertId() {
        return mIds[mId];
    }
}

@@ -1344,16 +1344,54 @@
 yList;%0A%0A
+import net.sourceforge.servestream.R;%0A
 import n
@@ -1921,39 +1921,8 @@
 ds;%0A
-%09private String %5B%5D mNicknames;%0A
 %09%0A  
@@ -2630,79 +2630,77 @@
 ;%0A%09%09
-mNicknames = new String%5Bstreams.size() + 1%5D;%0A%09%09%0A%09%09entries%5B0%5D = %22Silent%22
+%0A%09%09entries%5B0%5D = getContext().getString(R.string.silent_alarm_summary)
 ;%0A%09%09
@@ -2756,36 +2756,8 @@
  0;%0A
-%09%09mNicknames%5B0%5D = %22Silent%22;%0A
 %09%09%0A%09
@@ -2948,61 +2948,8 @@
 ();%0A
-%09%09%09mNicknames%5Bi + 1%5D = streams.get(i).getNickname();%0A
 %09%09%7D%0A
@@ -3088,32 +3088,85 @@
 sitiveResult) %7B%0A
+        CharSequence%5B%5D entries = getEntries();%0A    %09%0A
         if (posi
@@ -3224,32 +3224,29 @@
  setSummary(
-mNicknam
+entri
 es%5BmId%5D);%0A  
@@ -3701,16 +3701,66 @@
 t id) %7B%0A
+        CharSequence%5B%5D entries = getEntries();%0A%09%09%0A
 %09%09for (i
@@ -3874,16 +3874,13 @@
 ary(
-mNicknam
+entri
 es%5Bm

Cleaned up code