com.kaku.weac.fragment.RingSelectFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.kaku.weac.fragment.RingSelectFragment.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.kaku.weac.fragment;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.astuetz.PagerSlidingTabStrip;
import com.kaku.weac.R;
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.List;

/**
 * fragment
 *
 * @author 
 * @version 1.0 2015/05/18
 */
public class RingSelectFragment extends BaseFragment implements OnClickListener {

    /**
     * ??
     */
    private List<Fragment> mFragmentList;

    /**
     * ??
     */
    public static String sRingName;

    /**
     * ?
     */
    public static String sRingUrl;

    /**
     * ?
     */
    public static int sRingPager;

    /**
     * 
     */
    public static int sRingRequestType;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // ?Activity???
        Intent intent = getActivity().getIntent();
        sRingName = intent.getStringExtra(WeacConstants.RING_NAME);
        sRingUrl = intent.getStringExtra(WeacConstants.RING_URL);
        sRingPager = intent.getIntExtra(WeacConstants.RING_PAGER, -1);
        sRingRequestType = intent.getIntExtra(WeacConstants.RING_REQUEST_TYPE, 0);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fm_ring_select, container, false);
        // ?
        ViewGroup viewGroup = (ViewGroup) view.findViewById(R.id.ring_select_llyt);
        // ?
        MyUtil.setBackground(viewGroup, getActivity());

        // Fragment
        initFragment();
        // ViewPager
        initViewPager(view);

        // 
        ImageView actionCancel = (ImageView) view.findViewById(R.id.ring_select_cancel);
        actionCancel.setOnClickListener(this);

        // ?
        TextView actionSave = (TextView) view.findViewById(R.id.ring_select_save);
        actionSave.setOnClickListener(this);

        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // ?
        if (!AudioPlayer.sIsRecordStopMusic) {
            // ?
            AudioPlayer.getInstance(getActivity()).stop();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        // 
        case R.id.ring_select_cancel:
            getActivity().finish();
            break;
        // ?
        case R.id.ring_select_save:
            // ??
            String ringName = RingSelectItem.getInstance().getName();
            String ringUrl = RingSelectItem.getInstance().getUrl();
            int ringPager = RingSelectItem.getInstance().getRingPager();

            // ??
            SharedPreferences share = getActivity().getSharedPreferences(WeacConstants.EXTRA_WEAC_SHARE,
                    Activity.MODE_PRIVATE);
            SharedPreferences.Editor edit = share.edit();

            // ?
            if (sRingRequestType == 0) {
                edit.putString(WeacConstants.RING_NAME, ringName);
                edit.putString(WeacConstants.RING_URL, ringUrl);
                edit.putInt(WeacConstants.RING_PAGER, ringPager);
                // 
            } else {
                edit.putString(WeacConstants.RING_NAME_TIMER, ringName);
                edit.putString(WeacConstants.RING_URL_TIMER, ringUrl);
                edit.putInt(WeacConstants.RING_PAGER_TIMER, ringPager);
            }
            edit.apply();

            // ?
            Intent i = new Intent();
            i.putExtra(WeacConstants.RING_NAME, ringName);
            i.putExtra(WeacConstants.RING_URL, ringUrl);
            i.putExtra(WeacConstants.RING_PAGER, ringPager);
            getActivity().setResult(Activity.RESULT_OK, i);
            getActivity().finish();
            break;

        }

    }

    /**
     * Fragment
     */
    private void initFragment() {
        // Fragment
        SystemRingFragment systemRingFragment = new SystemRingFragment();
        // Fragment
        RecorderFragment recordFragment = new RecorderFragment();
        // Fragment
        LocalMusicFragment localMusicFragment = new LocalMusicFragment();

        mFragmentList = new ArrayList<>();
        mFragmentList.add(systemRingFragment);
        mFragmentList.add(localMusicFragment);
        mFragmentList.add(recordFragment);
    }

    /**
     * ViewPager??PagerSlidingTabStrip?
     *
     * @param view view
     */
    private void initViewPager(View view) {
        //      ?
        ViewPager viewPager = (ViewPager) view.findViewById(R.id.fragment_ring_select_sort);
        viewPager.setAdapter(new MyFragmentPagerAdapter(getActivity().getSupportFragmentManager()));
        viewPager.setOffscreenPageLimit(2);

        // ??
        int currentIndex;
        // ?
        if (sRingPager != -1) {
            // ????
            viewPager.setCurrentItem(sRingPager);
            currentIndex = sRingPager;
        } else {
            // ????
            SharedPreferences shares = getActivity().getSharedPreferences(WeacConstants.EXTRA_WEAC_SHARE,
                    Activity.MODE_PRIVATE);
            int position = shares.getInt(WeacConstants.RING_PAGER, 0);
            viewPager.setCurrentItem(position);
            currentIndex = position;
        }

        PagerSlidingTabStrip strip = (PagerSlidingTabStrip) view.findViewById(R.id.tabstrip);
        strip.setViewPager(viewPager);
        // ?????
        strip.setCurrentIndex(currentIndex);
        // 
        strip.setTypeface(Typeface.DEFAULT, 0);
    }

    /**
     * ?ViewPager?
     */
    private class MyFragmentPagerAdapter extends FragmentPagerAdapter {

        /**
         * 
         */
        private final String[] titles = { getString(R.string.system_ring), getString(R.string.local_music),
                getString(R.string.record) };

        public MyFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        // ?PagerSlidingTabStrip??
        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

    }
}