Java tutorial
/* * Copyright (C) 2014 Andrew Comminos * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.terracom.mumbleclient.channel; import android.content.SharedPreferences; import android.content.res.TypedArray; import android.os.Bundle; import android.os.RemoteException; import android.preference.PreferenceManager; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.PagerTabStrip; import android.support.v4.view.ViewPager; import android.util.TypedValue; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import com.terracom.jumble.IJumbleObserver; import com.terracom.jumble.model.User; import com.terracom.jumble.util.JumbleObserver; import com.terracom.mumbleclient.R; import com.terracom.mumbleclient.Settings; import com.terracom.mumbleclient.util.JumbleServiceFragment; import java.util.ArrayList; import java.util.List; /** * Class to encapsulate both a ChannelListFragment and ChannelChatFragment. * Created by andrew on 02/08/13. */ public class ChannelFragment extends JumbleServiceFragment implements SharedPreferences.OnSharedPreferenceChangeListener, ChatTargetProvider { private ViewPager mViewPager; private PagerTabStrip mTabStrip; private Button mTalkButton; private View mTalkView; private ChatTarget mChatTarget; /** Chat target listeners, notified when the chat target is changed. */ private List<OnChatTargetSelectedListener> mChatTargetListeners = new ArrayList<OnChatTargetSelectedListener>(); private boolean mTogglePTT; private JumbleObserver mObserver = new JumbleObserver() { @Override public void onUserTalkStateUpdated(User user) throws RemoteException { if (user != null && user.getSession() == getService().getSession()) { // Manually set button selection colour when we receive a talk state update. // This allows representation of talk state when using hot corners and PTT toggle. switch (user.getTalkState()) { case TALKING: case SHOUTING: case WHISPERING: mTalkButton.setPressed(true); break; case PASSIVE: mTalkButton.setPressed(false); break; } } } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_channel, container, false); mViewPager = (ViewPager) view.findViewById(R.id.channel_view_pager); mTabStrip = (PagerTabStrip) view.findViewById(R.id.channel_tab_strip); if (mTabStrip != null) { int[] attrs = new int[] { R.attr.colorPrimary, android.R.attr.textColorPrimaryInverse }; TypedArray a = getActivity().obtainStyledAttributes(attrs); int titleStripBackground = a.getColor(0, -1); int titleStripColor = a.getColor(1, -1); a.recycle(); mTabStrip.setTextColor(titleStripColor); mTabStrip.setTabIndicatorColor(titleStripColor); mTabStrip.setBackgroundColor(titleStripBackground); mTabStrip.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12); } mTalkView = view.findViewById(R.id.pushtotalk_view); mTalkButton = (Button) view.findViewById(R.id.pushtotalk); mTalkButton.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { try { boolean oldState = getService().isTalking(); boolean newState; if (event.getAction() == MotionEvent.ACTION_DOWN) { newState = !mTogglePTT || !oldState; } else if (event.getAction() == MotionEvent.ACTION_UP) { newState = mTogglePTT && oldState; } else { return true; } if (newState != oldState) { getService().setTalkingState(newState); } } catch (RemoteException e) { e.printStackTrace(); } return true; } }); configureInput(); return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); preferences.registerOnSharedPreferenceChangeListener(this); if (mViewPager != null) { // Phone ChannelFragmentPagerAdapter pagerAdapter = new ChannelFragmentPagerAdapter(getChildFragmentManager()); mViewPager.setAdapter(pagerAdapter); } else { // Tablet ChannelListFragment listFragment = new ChannelListFragment(); Bundle listArgs = new Bundle(); listArgs.putBoolean("pinned", isShowingPinnedChannels()); listFragment.setArguments(listArgs); ChannelChatFragment chatFragment = new ChannelChatFragment(); getChildFragmentManager().beginTransaction().replace(R.id.list_fragment, listFragment) .replace(R.id.chat_fragment, chatFragment).commit(); } } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); //inflater.inflate(R.menu.channel_menu, menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { Settings settings = Settings.getInstance(getActivity()); switch (item.getItemId()) { case R.id.menu_input_voice: settings.setInputMethod(Settings.ARRAY_INPUT_METHOD_VOICE); return true; case R.id.menu_input_ptt: settings.setInputMethod(Settings.ARRAY_INPUT_METHOD_PTT); return true; case R.id.menu_input_continuous: settings.setInputMethod(Settings.ARRAY_INPUT_METHOD_CONTINUOUS); return true; } return super.onOptionsItemSelected(item); } @Override public void onDestroy() { SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); preferences.unregisterOnSharedPreferenceChangeListener(this); super.onDestroy(); } @Override public IJumbleObserver getServiceObserver() { return mObserver; } /** * @return true if the channel fragment is set to display only the user's pinned channels. */ private boolean isShowingPinnedChannels() { return getArguments() != null && getArguments().getBoolean("pinned"); } /** * Configures the fragment in accordance with the user's interface preferences. */ private void configureInput() { Settings settings = Settings.getInstance(getActivity()); boolean showPttButton = settings.isPushToTalkButtonShown() && settings.getInputMethod().equals(Settings.ARRAY_INPUT_METHOD_PTT); mTalkView.setVisibility(showPttButton ? View.VISIBLE : View.GONE); mTogglePTT = settings.isPushToTalkToggle(); } @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { if (Settings.PREF_INPUT_METHOD.equals(key) || Settings.PREF_PUSH_BUTTON_HIDE_KEY.equals(key) || Settings.PREF_PTT_TOGGLE.equals(key)) configureInput(); } @Override public ChatTarget getChatTarget() { return mChatTarget; } @Override public void setChatTarget(ChatTarget target) { mChatTarget = target; for (OnChatTargetSelectedListener listener : mChatTargetListeners) listener.onChatTargetSelected(target); } @Override public void registerChatTargetListener(OnChatTargetSelectedListener listener) { mChatTargetListeners.add(listener); } @Override public void unregisterChatTargetListener(OnChatTargetSelectedListener listener) { mChatTargetListeners.remove(listener); } private class ChannelFragmentPagerAdapter extends FragmentPagerAdapter { public ChannelFragmentPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int i) { Fragment fragment = null; Bundle args = new Bundle(); switch (i) { case 0: fragment = new ChannelListFragment(); args.putBoolean("pinned", isShowingPinnedChannels()); break; case 1: fragment = new ChannelChatFragment(); break; } fragment.setArguments(args); return fragment; } @Override public CharSequence getPageTitle(int position) { switch (position) { case 0: return getString(R.string.channel).toUpperCase(); case 1: return getString(R.string.chat).toUpperCase(); default: return null; } } @Override public int getCount() { return 2; } } }