com.zhu.searchexternalstoragedirectory.LocalMusicFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.zhu.searchexternalstoragedirectory.LocalMusicFragment.java

Source

/*
 * Copyright (c) 2016  <kaku201313@163.com | 3772304@qq.com>
 *
 * 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 com.zhu.searchexternalstoragedirectory;

import android.app.Activity;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

/**
 * ?Fragment
 *
 * @author 
 * @version 1.0 2015/05
 */
public class LocalMusicFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {

    /**
     * ??Adapter
     */
    RingSelectAdapter mLocalMusicAdapter;

    /**
     * loader Id
     */
    private static final int LOADER_ID = 1;

    /**
     * ?
     */
    private int mPosition = 0;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fm_ring_local_music, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // ?cursor
        LoaderManager loaderManager = getLoaderManager();
        // Loader
        loaderManager.initLoader(LOADER_ID, null, this);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Map<String, String> map = mLocalMusicAdapter.getItem(position);
        // ???
        String ringName = map.get(WeacConstants.RING_NAME);
        // ??
        String ringUrl = map.get(WeacConstants.RING_URL);

        // ?
        mLocalMusicAdapter.notifyDataSetChanged();
        // ?????

        // 
        AudioPlayer.getInstance(getActivity()).play(ringUrl, false, false);

    }

    @Override
    public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
        // 
        return new CursorLoader(getActivity(), MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                new String[] { MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.DATA }, null, null,
                MediaStore.Audio.Media.DISPLAY_NAME);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        switch (loader.getId()) {
        case LOADER_ID:
            // ??????
            String ringName1;

            // ??List
            List<Map<String, String>> list = new ArrayList<>();
            // ??Set
            HashSet<String> set = new HashSet<>();

            if (cursor != null) {
                for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                    // ??
                    String ringName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                    // ??
                    String ringUrl = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
                    if (ringName != null) {
                        // ?????[.amr]?
                        if (!set.contains(ringName) && !ringUrl.contains("/WeaAlarmClock/audio/record")
                                && !ringName.equals("record_start.mp3") && !ringName.equals("record_stop.mp3")
                                && !ringName.equals("ring_weac_alarm_clock_default.mp3")) {
                            // ???
                            set.add(ringName);
                            // ??
                            ringName = MyUtil.removeEx(ringName);
                            Map<String, String> map = new HashMap<>();
                            map.put(WeacConstants.RING_NAME, ringName);
                            map.put(WeacConstants.RING_URL, ringUrl);
                            list.add(map);
                            //                                // ????
                            //                                if (ringName.equals(ringName1)) {
                            //                                    mPosition = list.size() - 1;
                            //                                    RingSelectItem.getInstance().setRingPager(1);
                            //                                }
                        }
                    }
                }
            }

            mLocalMusicAdapter = new RingSelectAdapter(getActivity(), list);
            setListAdapter(mLocalMusicAdapter);
            setSelection(mPosition);
            break;
        }
    }

    @Override
    public void onLoaderReset(Loader<Cursor> arg0) {

    }

}