1/*
2 * Copyright (C) 2007 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.music;
18
19import android.Manifest.permission;
20import android.content.pm.PackageManager;
21import com.android.music.MusicUtils.ServiceToken;
22
23import android.app.Activity;
24import android.content.ComponentName;
25import android.content.ServiceConnection;
26import android.os.Bundle;
27import android.os.IBinder;
28import android.os.RemoteException;
29
30public class MusicBrowserActivity extends Activity implements MusicUtils.Defs {
31    private ServiceToken mToken;
32    private static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 42;
33
34    public MusicBrowserActivity() {}
35
36    /**
37     * Called when the activity is first created.
38     */
39    @Override
40    public void onCreate(Bundle icicle) {
41        super.onCreate(icicle);
42        if (checkSelfPermission(permission.READ_EXTERNAL_STORAGE)
43                != PackageManager.PERMISSION_GRANTED) {
44            requestPermissions(new String[] {permission.READ_EXTERNAL_STORAGE},
45                    MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
46            return;
47        }
48        initApp();
49    }
50
51    public void initApp() {
52        int activeTab = MusicUtils.getIntPref(this, "activetab", R.id.artisttab);
53        if (activeTab != R.id.artisttab && activeTab != R.id.albumtab && activeTab != R.id.songtab
54                && activeTab != R.id.playlisttab) {
55            activeTab = R.id.artisttab;
56        }
57        MusicUtils.activateTab(this, activeTab);
58
59        String shuf = getIntent().getStringExtra("autoshuffle");
60        if ("true".equals(shuf)) {
61            mToken = MusicUtils.bindToService(this, autoshuffle);
62        }
63    }
64
65    @Override
66    public void onDestroy() {
67        if (mToken != null) {
68            MusicUtils.unbindFromService(mToken);
69        }
70        super.onDestroy();
71    }
72
73    private ServiceConnection autoshuffle = new ServiceConnection() {
74        public void onServiceConnected(ComponentName classname, IBinder obj) {
75            // we need to be able to bind again, so unbind
76            try {
77                unbindService(this);
78            } catch (IllegalArgumentException e) {
79            }
80            IMediaPlaybackService serv = IMediaPlaybackService.Stub.asInterface(obj);
81            if (serv != null) {
82                try {
83                    serv.setShuffleMode(MediaPlaybackService.SHUFFLE_AUTO);
84                } catch (RemoteException ex) {
85                }
86            }
87        }
88
89        public void onServiceDisconnected(ComponentName classname) {}
90    };
91
92    @Override
93    public void onRequestPermissionsResult(
94            int requestCode, String permissions[], int[] grantResults) {
95        switch (requestCode) {
96            case MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE: {
97                if (grantResults.length == 0
98                        || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
99                    finish();
100                    return;
101                }
102                initApp();
103            }
104        }
105    }
106}
107