Home | History | Annotate | Download | only in musicfx
      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 
     17 package com.android.musicfx;
     18 
     19 import com.android.internal.app.AlertActivity;
     20 import com.android.internal.app.AlertController;
     21 import com.android.internal.app.AlertController.AlertParams.OnPrepareListViewListener;
     22 import com.android.musicfx.Compatibility.Service;
     23 
     24 import android.content.DialogInterface;
     25 import android.content.DialogInterface.OnClickListener;
     26 import android.content.Intent;
     27 import android.content.SharedPreferences;
     28 import android.content.pm.PackageManager;
     29 import android.content.pm.ResolveInfo;
     30 import android.database.Cursor;
     31 import android.database.MatrixCursor;
     32 import android.media.audiofx.AudioEffect;
     33 import android.os.Bundle;
     34 import android.util.Log;
     35 import android.view.View;
     36 import android.widget.AdapterView;
     37 import android.widget.AdapterView.OnItemSelectedListener;
     38 import android.widget.ListView;
     39 
     40 import java.util.List;
     41 
     42 /**
     43  * shows a dialog that lets the user switch between control panels
     44  */
     45 public class ControlPanelPicker extends AlertActivity implements OnClickListener, OnPrepareListViewListener {
     46 
     47 
     48     int mClickedPos = -1;
     49 
     50     @Override
     51     public void onCreate(final Bundle savedInstanceState) {
     52         super.onCreate(savedInstanceState);
     53 
     54         String [] cols = new String [] { "_id", "title", "package", "name" };
     55         MatrixCursor c = new MatrixCursor(cols);
     56 
     57         PackageManager pmgr = getPackageManager();
     58         Intent i = new Intent(AudioEffect.ACTION_DISPLAY_AUDIO_EFFECT_CONTROL_PANEL);
     59         List<ResolveInfo> ris = pmgr.queryIntentActivities(i, PackageManager.GET_DISABLED_COMPONENTS);
     60         SharedPreferences pref = getSharedPreferences("musicfx", MODE_PRIVATE);
     61         String savedDefPackage = pref.getString("defaultpanelpackage", null);
     62         String savedDefName = pref.getString("defaultpanelname", null);
     63         int cnt = -1;
     64         int defpanelidx = 0;
     65         for (ResolveInfo foo: ris) {
     66             if (foo.activityInfo.name.equals(Compatibility.Redirector.class.getName())) {
     67                 continue;
     68             }
     69             CharSequence name = pmgr.getApplicationLabel(foo.activityInfo.applicationInfo);
     70             c.addRow(new Object [] { 0, name, foo.activityInfo.packageName, foo.activityInfo.name });
     71             cnt += 1;
     72             if (foo.activityInfo.name.equals(savedDefName) &&
     73                     foo.activityInfo.packageName.equals(savedDefPackage) &&
     74                     foo.activityInfo.enabled) {
     75                 // mark as default in the list
     76                 defpanelidx = cnt;
     77             }
     78         }
     79 
     80         final AlertController.AlertParams p = mAlertParams;
     81         p.mCursor = c;
     82         p.mOnClickListener = mItemClickListener;
     83         p.mLabelColumn = "title";
     84         p.mIsSingleChoice = true;
     85         p.mPositiveButtonText = getString(com.android.internal.R.string.ok);
     86         p.mPositiveButtonListener = this;
     87         p.mNegativeButtonText = getString(com.android.internal.R.string.cancel);
     88         p.mOnPrepareListViewListener = this;
     89         p.mTitle = getString(R.string.picker_title);
     90         p.mCheckedItem = defpanelidx;
     91 
     92         setupAlert();
     93     }
     94 
     95     private DialogInterface.OnClickListener mItemClickListener =
     96         new DialogInterface.OnClickListener() {
     97 
     98         public void onClick(DialogInterface dialog, int which) {
     99             // Save the position of most recently clicked item
    100             mClickedPos = which;
    101         }
    102 
    103     };
    104 
    105     @Override
    106     public void onClick(DialogInterface dialog, int which) {
    107         if (which == DialogInterface.BUTTON_POSITIVE) {
    108             // set new default
    109             Intent updateIntent = new Intent(this, Service.class);
    110             Cursor c = mAlertParams.mCursor;
    111             c.moveToPosition(mClickedPos);
    112             updateIntent.putExtra("defPackage", c.getString(2));
    113             updateIntent.putExtra("defName", c.getString(3));
    114             startService(updateIntent);
    115         }
    116     }
    117 
    118     @Override
    119     public void onPrepareListView(ListView listView) {
    120         //mAlertParams.mCheckedItem = mDefPanelPos;
    121     }
    122 }
    123