1/*
2 * Copyright (C) 2010-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 android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.media.audiofx.AudioEffect;
23import android.os.Bundle;
24import android.util.Log;
25
26import java.util.HashMap;
27
28public class ControlPanelReceiver extends BroadcastReceiver {
29
30    private final static String TAG = "MusicFXControlPanelReceiver";
31
32    @Override
33    public void onReceive(final Context context, final Intent intent) {
34
35        Log.v(TAG, "onReceive");
36
37        if ((context == null) || (intent == null)) {
38            Log.w(TAG, "Context or intent is null. Do nothing.");
39            return;
40        }
41
42        final String action = intent.getAction();
43        final String packageName = intent.getStringExtra(AudioEffect.EXTRA_PACKAGE_NAME);
44        final int audioSession = intent.getIntExtra(AudioEffect.EXTRA_AUDIO_SESSION,
45                AudioEffect.ERROR_BAD_VALUE);
46
47        Log.v(TAG, "Action: " + action);
48        Log.v(TAG, "Package name: " + packageName);
49        Log.v(TAG, "Audio session: " + audioSession);
50
51        // check package name
52        if (packageName == null) {
53            Log.w(TAG, "Null package name");
54            return;
55        }
56
57        // check audio session
58        if ((audioSession == AudioEffect.ERROR_BAD_VALUE) || (audioSession < 0)) {
59            Log.w(TAG, "Invalid or missing audio session " + audioSession);
60            return;
61        }
62
63        // open audio session
64        if (action.equals(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION)) {
65
66            // retrieve the effect enabled state
67            final boolean isGlobalEnabled = context.getSharedPreferences(packageName,
68                    Context.MODE_PRIVATE).getBoolean(
69                    ControlPanelEffect.Key.global_enabled.toString(),
70                    ControlPanelEffect.GLOBAL_ENABLED_DEFAULT);
71
72            ControlPanelEffect.openSession(context, packageName, audioSession);
73        }
74
75        // close audio session
76        if (action.equals(AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION)) {
77
78            ControlPanelEffect.closeSession(context, packageName, audioSession);
79        }
80
81        // set params
82        if (action.equals("AudioEffect.ACTION_SET_PARAM")) {
83            final String param = intent.getStringExtra("AudioEffect.EXTRA_PARAM");
84
85            if (param.equals("GLOBAL_ENABLED")) {
86                final Boolean value = intent.getBooleanExtra("AudioEffect.EXTRA_VALUE", false);
87                ControlPanelEffect.setParameterBoolean(context, packageName, audioSession,
88                        ControlPanelEffect.Key.global_enabled, value);
89            }
90        }
91
92        // get params
93        if (action.equals("AudioEffect.ACTION_GET_PARAM")) {
94            final String param = intent.getStringExtra("AudioEffect.EXTRA_PARAM");
95
96            if (param.equals("GLOBAL_ENABLED")) {
97                final Boolean value = ControlPanelEffect.getParameterBoolean(context, packageName,
98                        audioSession, ControlPanelEffect.Key.global_enabled);
99                final Bundle extras = new Bundle();
100                extras.putBoolean("GLOBAL_ENABLED", value);
101                setResultExtras(extras);
102            }
103        }
104    }
105}
106