Android Music源碼解析(一)

Music代碼結(jié)構(gòu)

Music是Android源碼中自帶的一個(gè)音樂(lè)播放器,算是一個(gè)輕量級(jí)的應(yīng)用,如下圖所示。


Music首頁(yè)

??在Music源碼分析的第一篇,我們首先看一看Music源碼在android中的結(jié)構(gòu):


源碼結(jié)構(gòu).png

??大致掃一下代碼名稱,我們能看出部分源碼相對(duì)比較簡(jiǎn)單,代碼量也不大,正好作為我們進(jìn)軍Anodroid源碼的第一步~那么接下來(lái),我們將從程序入口開(kāi)始一步步分析代碼,最后再根據(jù)前面的分析來(lái)進(jìn)行代碼分類和總結(jié)。

MusicBrowserActivity

首先我們從AndroidManifest.xml中找到如下一段代碼,很容易就能確定入口的Activity是MusicBrowserActivity。其中android:exported="true"表明可以由其他程序訪問(wèn)。

    <activity android:name="com.android.music.MusicBrowserActivity"
        android:theme="@android:style/Theme.NoTitleBar"
        android:exported="true"
    >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <action android:name="android.intent.action.MUSIC_PLAYER" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

這里可以注意一下,MusicBrowserActivity實(shí)現(xiàn)了MusicUtils.Defs接口,這個(gè)接口實(shí)質(zhì)上是一系列常量:

public interface Defs {
    public final static int OPEN_URL = 0;
    public final static int ADD_TO_PLAYLIST = 1;
    public final static int USE_AS_RINGTONE = 2;
    public final static int PLAYLIST_SELECTED = 3;
    public final static int NEW_PLAYLIST = 4;
    public final static int PLAY_SELECTION = 5;
    public final static int GOTO_START = 6;
    public final static int GOTO_PLAYBACK = 7;
    public final static int PARTY_SHUFFLE = 8;
    public final static int SHUFFLE_ALL = 9;
    public final static int DELETE_ITEM = 10;
    public final static int SCAN_DONE = 11;
    public final static int QUEUE = 12;
    public final static int EFFECTS_PANEL = 13;
    public final static int CHILD_MENU_BASE = 14; // this should be the last item
}

接下來(lái)我們看向MusicBrowserActivity,其代碼結(jié)構(gòu)如下:

MusicBrowserActivity代碼結(jié)構(gòu).jpeg

??可以看到這個(gè)Activity下面包含一個(gè)構(gòu)造函數(shù),兩個(gè)成員方法和兩個(gè)成員變量。其中,構(gòu)造函數(shù)是個(gè)空方法,我們主要研究其他的部分。
??首先關(guān)注一下成員變量。

private ServiceToken mToken;

點(diǎn)進(jìn)去以后可以發(fā)現(xiàn),ServiceToken是一個(gè)定義在MusicUtils中的靜態(tài)內(nèi)部類。

public static class ServiceToken {
    ContextWrapper mWrappedContext;
    ServiceToken(ContextWrapper context) {
        mWrappedContext = context;
    }
}

可以看到,ServiceToken的實(shí)質(zhì)就是ContextWrapper,即Context的一個(gè)裝飾器(裝飾模式)。
??接下來(lái)是另一個(gè)內(nèi)部類autoshuffle的定義。顯然,這是一個(gè)典型的AIDL的客戶端實(shí)現(xiàn),它綁定了一個(gè)遠(yuǎn)程服務(wù)。這個(gè)遠(yuǎn)程服務(wù)已在本地定義,我們下一章會(huì)具體分析服務(wù)端的實(shí)現(xiàn)。

private ServiceConnection autoshuffle = new ServiceConnection() {
    public void onServiceConnected(ComponentName classname, IBinder obj) {
        // we need to be able to bind again, so unbind
        try {
            unbindService(this);
        } catch (IllegalArgumentException e) {
        }
        IMediaPlaybackService serv = IMediaPlaybackService.Stub.asInterface(obj);
        if (serv != null) {
            try {
                serv.setShuffleMode(MediaPlaybackService.SHUFFLE_AUTO);
            } catch (RemoteException ex) {
            }
        }
    }

    public void onServiceDisconnected(ComponentName classname) {
    }
};?

在這個(gè)Acitivity中,它重寫(xiě)了onCreate和onDestory。其中,onCreate中主要做了兩件事情。我們首先先看這一段代碼:

    int activeTab = MusicUtils.getIntPref(this, "activetab",     R.id.artisttab);
    if (activeTab != R.id.artisttab
            && activeTab != R.id.albumtab
            && activeTab != R.id.songtab
            && activeTab != R.id.playlisttab) {
        activeTab = R.id.artisttab;
    }
    MusicUtils.activateTab(this, activeTab);

首先,我們跟蹤getIntPref方法。如下所示,這里調(diào)用的實(shí)質(zhì)是獲取了一個(gè)SharedPreferences,并得到其中存儲(chǔ)的activeTab值。

static int getIntPref(Context context, String name, int def) {
    SharedPreferences prefs =
        context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
    return prefs.getInt(name, def);
}

接下來(lái)確定activeTab的取值,默認(rèn)設(shè)為R.id.artisttab。很容易發(fā)現(xiàn),這里是用于設(shè)置當(dāng)前顯示的選項(xiàng)卡信息。完成這一步后,又會(huì)調(diào)用MusicUtils中的activateTab方法,這個(gè)方法具體代碼如下:

static void activateTab(Activity a, int id) {
    //設(shè)置action
    Intent intent = new Intent(Intent.ACTION_PICK);
     //根據(jù)id設(shè)置URI和mimeType
    switch (id) {
        case R.id.artisttab:
            intent.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/artistalbum");
            break;
        case R.id.albumtab:
            intent.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/album");
            break;
        case R.id.songtab:
            intent.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/track");
            break;
        case R.id.playlisttab:
            intent.setDataAndType(Uri.EMPTY, MediaStore.Audio.Playlists.CONTENT_TYPE);
            break;
        case R.id.nowplayingtab:
            intent = new Intent(a, MediaPlaybackActivity.class);
            a.startActivity(intent);
            // fall through and return
        default:
            return;
    }
    intent.putExtra("withtabs", true);
    //設(shè)置標(biāo)記。擁有這個(gè)標(biāo)記的Activity在啟動(dòng)時(shí),同一個(gè)任務(wù)棧中所有位于它上面的Activity都要出棧。此時(shí)如果Activity為singleTask啟動(dòng)模式,則重啟時(shí)系統(tǒng)會(huì)調(diào)用onNewIntent;若是默認(rèn)的standard模式,則它連同它上面的Activity都要出棧,系統(tǒng)會(huì)創(chuàng)建新的Activity實(shí)例并放入棧頂。
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    a.startActivity(intent);
    a.finish();
    //設(shè)置切換activity動(dòng)畫(huà)。這里是沒(méi)有動(dòng)畫(huà)
    a.overridePendingTransition(0, 0);
}

首先吐槽一下這個(gè)Acticity a的命名……我估計(jì)我如果這么命名會(huì)被老大罵死=。=……好了,這部分代碼采用了activity的隱式調(diào)用,通過(guò)匹配各類字段啟動(dòng)相應(yīng)的Activity。下面的一小段代碼以為示例(可以對(duì)比action和mimeType)。

    <activity android:name="com.android.music.ArtistAlbumBrowserActivity" android:exported="false" >
        <intent-filter>
            <action android:name="android.intent.action.PICK" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="vnd.android.cursor.dir/artistalbum"/>
        </intent-filter>
    </activity>

onCreate方法的第二部分和onDestory對(duì)應(yīng),分別對(duì)應(yīng)著綁定和解綁service,代碼如下:

    //onCreate中第二部分綁定service代碼
    String shuf = getIntent().getStringExtra("autoshuffle");
    if ("true".equals(shuf)) {
        mToken = MusicUtils.bindToService(this, autoshuffle);
    }
   //onDestory中解綁service代碼
    if (mToken != null) {
        MusicUtils.unbindFromService(mToken);
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容