1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.musicfx;
18
19import com.android.internal.app.AlertActivity;
20import com.android.internal.app.AlertController;
21import com.android.internal.app.AlertController.AlertParams.OnPrepareListViewListener;
22import com.android.musicfx.Compatibility.Service;
23
24import android.content.DialogInterface;
25import android.content.DialogInterface.OnClickListener;
26import android.content.Intent;
27import android.content.SharedPreferences;
28import android.content.pm.PackageManager;
29import android.content.pm.ResolveInfo;
30import android.database.Cursor;
31import android.database.MatrixCursor;
32import android.media.audiofx.AudioEffect;
33import android.os.Bundle;
34import android.util.Log;
35import android.view.View;
36import android.widget.AdapterView;
37import android.widget.AdapterView.OnItemSelectedListener;
38import android.widget.ListView;
39
40import java.util.List;
41
42/**
43 * shows a dialog that lets the user switch between control panels
44 */
45public class ControlPanelPicker extends AlertActivity implements OnClickListener, OnPrepareListViewListener {
46
47
48
49    @Override
50    public void onCreate(final Bundle savedInstanceState) {
51        super.onCreate(savedInstanceState);
52
53        String [] cols = new String [] { "_id", "title", "package", "name" };
54        MatrixCursor c = new MatrixCursor(cols);
55
56        PackageManager pmgr = getPackageManager();
57        Intent i = new Intent(AudioEffect.ACTION_DISPLAY_AUDIO_EFFECT_CONTROL_PANEL);
58        List<ResolveInfo> ris = pmgr.queryIntentActivities(i, PackageManager.GET_DISABLED_COMPONENTS);
59        SharedPreferences pref = getSharedPreferences("musicfx", MODE_PRIVATE);
60        String savedDefPackage = pref.getString("defaultpanelpackage", null);
61        String savedDefName = pref.getString("defaultpanelname", null);
62        int cnt = -1;
63        int defpanelidx = 0;
64        for (ResolveInfo foo: ris) {
65            if (foo.activityInfo.name.equals(Compatibility.Redirector.class.getName())) {
66                continue;
67            }
68            CharSequence name = pmgr.getApplicationLabel(foo.activityInfo.applicationInfo);
69            c.addRow(new Object [] { 0, name, foo.activityInfo.packageName, foo.activityInfo.name });
70            cnt += 1;
71            if (foo.activityInfo.name.equals(savedDefName) &&
72                    foo.activityInfo.packageName.equals(savedDefPackage) &&
73                    foo.activityInfo.enabled) {
74                // mark as default in the list
75                defpanelidx = cnt;
76            }
77        }
78
79        final AlertController.AlertParams p = mAlertParams;
80        p.mCursor = c;
81        p.mOnClickListener = mItemClickListener;
82        p.mLabelColumn = "title";
83        p.mIsSingleChoice = true;
84        p.mPositiveButtonText = getString(com.android.internal.R.string.ok);
85        p.mPositiveButtonListener = this;
86        p.mNegativeButtonText = getString(com.android.internal.R.string.cancel);
87        p.mOnPrepareListViewListener = this;
88        p.mTitle = getString(R.string.picker_title);
89        p.mCheckedItem = defpanelidx;
90
91        setupAlert();
92    }
93
94    private DialogInterface.OnClickListener mItemClickListener =
95        new DialogInterface.OnClickListener() {
96
97        public void onClick(DialogInterface dialog, int which) {
98            // Save the position of most recently clicked item
99            mAlertParams.mCheckedItem = which;
100        }
101
102    };
103
104    @Override
105    public void onClick(DialogInterface dialog, int which) {
106        if (which == DialogInterface.BUTTON_POSITIVE) {
107            // set new default
108            Intent updateIntent = new Intent(this, Service.class);
109            Cursor c = mAlertParams.mCursor;
110            c.moveToPosition(mAlertParams.mCheckedItem);
111            updateIntent.putExtra("defPackage", c.getString(2));
112            updateIntent.putExtra("defName", c.getString(3));
113            startService(updateIntent);
114        }
115    }
116
117    @Override
118    public void onPrepareListView(ListView listView) {
119        //mAlertParams.mCheckedItem = mDefPanelPos;
120    }
121}
122