summaryrefslogtreecommitdiffstats
path: root/src/org/cyanogenmod/audiofx/activity/ControlPanelPicker.java
blob: 03e6b06bb626105bd5f1f31b163c6487f0288563 (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
/*
 * Copyright (C) 2011 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 org.cyanogenmod.audiofx.activity;

import com.android.internal.app.AlertActivity;
import com.android.internal.app.AlertController;
import com.android.internal.app.AlertController.AlertParams.OnPrepareListViewListener;
import org.cyanogenmod.audiofx.Compatibility;
import org.cyanogenmod.audiofx.Compatibility.Service;

import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.media.audiofx.AudioEffect;
import android.os.Bundle;
import android.widget.ListView;
import org.cyanogenmod.audiofx.R;

import java.util.List;

/**
 * shows a dialog that lets the user switch between control panels
 */
public class ControlPanelPicker extends AlertActivity implements OnClickListener, OnPrepareListViewListener {

    

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

        String [] cols = new String [] { "_id", "title", "package", "name" };
        MatrixCursor c = new MatrixCursor(cols);

        PackageManager pmgr = getPackageManager();
        Intent i = new Intent(AudioEffect.ACTION_DISPLAY_AUDIO_EFFECT_CONTROL_PANEL);
        List<ResolveInfo> ris = pmgr.queryIntentActivities(i, PackageManager.GET_DISABLED_COMPONENTS);
        SharedPreferences pref = getSharedPreferences("musicfx", MODE_PRIVATE);
        String savedDefPackage = pref.getString("defaultpanelpackage", null);
        String savedDefName = pref.getString("defaultpanelname", null);
        int cnt = -1;
        int defpanelidx = 0;
        for (ResolveInfo foo: ris) {
            if (foo.activityInfo.name.equals(Compatibility.Redirector.class.getName())) {
                continue;
            }
            CharSequence name = pmgr.getApplicationLabel(foo.activityInfo.applicationInfo);
            c.addRow(new Object [] { 0, name, foo.activityInfo.packageName, foo.activityInfo.name });
            cnt += 1;
            if (foo.activityInfo.name.equals(savedDefName) &&
                    foo.activityInfo.packageName.equals(savedDefPackage) &&
                    foo.activityInfo.enabled) {
                // mark as default in the list
                defpanelidx = cnt;
            }
        }
        
        final AlertController.AlertParams p = mAlertParams;
        p.mCursor = c;
        p.mOnClickListener = mItemClickListener;
        p.mLabelColumn = "title";
        p.mIsSingleChoice = true;
        p.mPositiveButtonText = getString(getOkStringResId());
        p.mPositiveButtonListener = this;
        p.mNegativeButtonText = getString(getCancelStringResId());
        p.mOnPrepareListViewListener = this;
        p.mTitle = getString(R.string.picker_title);
        p.mCheckedItem = defpanelidx;
        
        setupAlert();
    }

    private int getOkStringResId() {
        return getResources().getIdentifier("ok", "string", "android");
    }

    private int getCancelStringResId() {
        return getResources().getIdentifier("cancel", "string", "android");
    }

    private DialogInterface.OnClickListener mItemClickListener =
        new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            // Save the position of most recently clicked item
            mAlertParams.mCheckedItem = which;
        }
        
    };

    @Override
    public void onClick(DialogInterface dialog, int which) {
        if (which == DialogInterface.BUTTON_POSITIVE) {
            // set new default
            Intent updateIntent = new Intent(this, Service.class);
            Cursor c = mAlertParams.mCursor;
            c.moveToPosition(mAlertParams.mCheckedItem);
            updateIntent.putExtra("defPackage", c.getString(2));
            updateIntent.putExtra("defName", c.getString(3));
            startService(updateIntent);
        }
    }

    @Override
    public void onPrepareListView(ListView listView) {
        //mAlertParams.mCheckedItem = mDefPanelPos;
    }
}