Java tutorial
/* * 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.kaku.weac.fragment; 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.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 com.kaku.weac.R; import com.kaku.weac.adapter.RingSelectAdapter; import com.kaku.weac.bean.RingSelectItem; import com.kaku.weac.common.WeacConstants; import com.kaku.weac.util.AudioPlayer; import com.kaku.weac.util.MyUtil; 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 SystemRingFragment extends BaseListFragment implements LoaderManager.LoaderCallbacks<Cursor> { /** * ??Adapter */ RingSelectAdapter mSystemRingAdapter; /** * 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_system_ring, 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); // initAdapter(); } @Override public void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); Map<String, String> map = mSystemRingAdapter.getItem(position); // ??? String ringName = map.get(WeacConstants.RING_NAME); // ?? String ringUrl = map.get(WeacConstants.RING_URL); // ?? mSystemRingAdapter.updateSelection(ringName); // ? mSystemRingAdapter.notifyDataSetChanged(); // ???? RingSelectItem.getInstance().setRingPager(0); // switch (ringUrl) { case WeacConstants.DEFAULT_RING_URL: // AudioPlayer.getInstance(getActivity()).playRaw(R.raw.ring_weac_alarm_clock_default, false, false); // break; case WeacConstants.NO_RING_URL: AudioPlayer.getInstance(getActivity()).stop(); break; default: AudioPlayer.getInstance(getActivity()).play(ringUrl, false, false); break; } ViewPager pager = (ViewPager) getActivity().findViewById(R.id.fragment_ring_select_sort); PagerAdapter f = pager.getAdapter(); LocalMusicFragment localMusicFragment = (LocalMusicFragment) f.instantiateItem(pager, 1); RecorderFragment recorderFragment = (RecorderFragment) f.instantiateItem(pager, 2); // ?? if (localMusicFragment.mLocalMusicAdapter != null) { localMusicFragment.mLocalMusicAdapter.updateSelection(""); localMusicFragment.mLocalMusicAdapter.notifyDataSetChanged(); } // ? if (recorderFragment.mRecorderAdapter != null) { recorderFragment.mRecorderAdapter.updateSelection(""); recorderFragment.mRecorderAdapter.notifyDataSetChanged(); } } @Override public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) { // return new CursorLoader(getActivity(), MediaStore.Audio.Media.INTERNAL_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; if (RingSelectFragment.sRingName != null) { ringName1 = RingSelectFragment.sRingName; } else { SharedPreferences share = getActivity().getSharedPreferences(WeacConstants.EXTRA_WEAC_SHARE, Activity.MODE_PRIVATE); // ??????,? ringName1 = share.getString(WeacConstants.RING_NAME, getString(R.string.default_ring)); } // ??Set HashSet<String> set = new HashSet<>(); // ??List List<Map<String, String>> list = new ArrayList<>(); // Map<String, String> defaultRing = new HashMap<>(); defaultRing.put(WeacConstants.RING_NAME, getString(R.string.default_ring)); defaultRing.put(WeacConstants.RING_URL, WeacConstants.DEFAULT_RING_URL); list.add(defaultRing); set.add(getString(R.string.default_ring)); // ???? if (getString(R.string.default_ring).equals(ringName1)) { mPosition = 0; RingSelectItem.getInstance().setRingPager(0); } // Map<String, String> noRing = new HashMap<>(); noRing.put(WeacConstants.RING_NAME, getString(R.string.no_ring)); noRing.put(WeacConstants.RING_URL, WeacConstants.NO_RING_URL); list.add(noRing); set.add(getString(R.string.no_ring)); // ???? if (getString(R.string.no_ring).equals(ringName1)) { mPosition = list.size() - 1; RingSelectItem.getInstance().setRingPager(0); } if (cursor != null) { for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { // ?? String ringName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)); if (ringName != null) { // ?? if (!set.contains(ringName)) { // ??? set.add(ringName); // ?? ringName = MyUtil.removeEx(ringName); // ?? String ringUrl = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)); 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(0); } } } } } mSystemRingAdapter = new RingSelectAdapter(getActivity(), list, ringName1); setListAdapter(mSystemRingAdapter); setSelection(mPosition); break; } } @Override public void onLoaderReset(Loader<Cursor> arg0) { } }