/*
 * Copyright (c) 2009 Google Inc.  All Rights Reserved.
 *
 * 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 uk.org.ngo.squeezer;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import java.util.Arrays;

import uk.org.ngo.squeezer.framework.BaseActivity;

public class RandomplayActivity extends BaseActivity {

    private ListView listView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item_list);
        listView = (ListView) findViewById(R.id.item_list);
        setRandomplayMenu();
    }


    private void setRandomplayMenu() {
        String[] values = getResources().getStringArray(R.array.randomplay_items);
        int[] icons = new int[values.length];
        Arrays.fill(icons, R.drawable.ic_random);

        // XXX: Implement the "Choose the genres that the random mix will be
        // drawn from" functionality.
        // icons[icons.length - 1] = R.drawable.ic_genres;
        listView.setAdapter(new IconRowAdapter(this, values, icons));
        listView.setOnItemClickListener(onRandomplayItemClick);
    }

    private final OnItemClickListener onRandomplayItemClick = new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (position < RandomPlayType.values().length) {
                getService().randomPlay(RandomPlayType.values()[position].toString());
                NowPlayingActivity.show(RandomplayActivity.this);
            }
        }
    };

    static void show(Context context) {
        final Intent intent = new Intent(context, RandomplayActivity.class);
        context.startActivity(intent);
    }

    public enum RandomPlayType {
        tracks,
        albums,
        contributors,
        year
    }

}

@@ -736,16 +736,59 @@
 Bundle;%0A
+import android.support.annotation.NonNull;%0A
 import a
@@ -1004,16 +1004,68 @@
 ctivity;
+%0Aimport uk.org.ngo.squeezer.service.ISqueezeService;
 %0A%0Apublic
@@ -1354,32 +1354,157 @@
 .id.item_list);%0A
+    %7D%0A%0A    protected void onServiceConnected(@NonNull ISqueezeService service) %7B%0A        super.onServiceConnected(service);%0A%0A
         setRando
@@ -1504,25 +1504,32 @@
 etRandom
-playMenu(
+PlayList(service
 );%0A    %7D
@@ -1530,17 +1530,16 @@
 %0A    %7D%0A%0A
-%0A
     priv
@@ -1560,17 +1560,49 @@
 ndom
-playMenu(
+PlayList(@NonNull ISqueezeService service
 ) %7B%0A
@@ -2069,146 +2069,449 @@
 ner(
-o
+new O
 nRandom
-p
+P
 lay
-Item
 Click
-);%0A    %7D%0A%0A    private final OnItemClickListener o
+Listener(service));%0A    %7D%0A%0A    /**%0A     * Provides an OnItemClickListener where the onItemClick() method is known to require%0A     * a functioning ISqueezeService.%0A     */%0A    private class O
 nRandom
-playItemClick = new OnItemClickListener() %7B%0A        @Override
+PlayClickListener implements OnItemClickListener %7B%0A        @NonNull protected final ISqueezeService mService;%0A%0A        OnRandomPlayClickListener(@NonNull ISqueezeService service) %7B%0A            mService = service;%0A        %7D%0A
 %0A   
@@ -2679,20 +2679,16 @@
     
-get
+m
 Service
-()
 .ran
@@ -2837,17 +2837,16 @@
  %7D%0A    %7D
-;
 %0A%0A    st

Handle service NPEs in RandomPlayActivity.