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.morlunk.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 android.widget.CheckBox; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import com.morlunk.jumble.IJumbleService; import com.morlunk.jumble.JumbleService; import com.morlunk.jumble.model.IUser; import com.morlunk.jumble.model.User; import com.morlunk.jumble.model.WhisperTarget; import com.morlunk.jumble.util.IJumbleObserver; import com.morlunk.jumble.util.JumbleObserver; import com.morlunk.jumble.util.VoiceTargetMode; import com.morlunk.mumbleclient.R; import com.morlunk.mumbleclient.Settings; import com.morlunk.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 View mTargetPanel; private ImageView mTargetPanelCancel; private TextView mTargetPanelText; private ChatTarget mChatTarget; /** Chat target listeners, notified when the chat target is changed. */ private List<OnChatTargetSelectedListener> mChatTargetListeners = new ArrayList<OnChatTargetSelectedListener>(); private JumbleObserver mObserver = new JumbleObserver() { @Override public void onUserTalkStateUpdated(IUser user) { 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 onVoiceTargetChanged(VoiceTargetMode mode) { configureTargetPanel(); } }; @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) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: getService().onTalkKeyDown(); break; case MotionEvent.ACTION_UP: getService().onTalkKeyUp(); break; } return true; } }); mTargetPanel = view.findViewById(R.id.target_panel); mTargetPanelCancel = (ImageView) view.findViewById(R.id.target_panel_cancel); mTargetPanelCancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (getService() != null && getService().getConnectionState() == JumbleService.ConnectionState.CONNECTED && getService().getVoiceTargetMode() == VoiceTargetMode.WHISPER) { byte target = getService().getVoiceTargetId(); getService().setVoiceTargetId((byte) 0); getService().unregisterWhisperTarget(target); } } }); mTargetPanelText = (TextView) view.findViewById(R.id.target_panel_warning); 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; } @Override public void onServiceBound(IJumbleService service) { super.onServiceBound(service); configureTargetPanel(); } private void configureTargetPanel() { VoiceTargetMode mode = getService().getVoiceTargetMode(); if (mode == VoiceTargetMode.WHISPER) { WhisperTarget target = getService().getWhisperTarget(); mTargetPanel.setVisibility(View.VISIBLE); mTargetPanelText.setText(getString(R.string.shout_target, target.getName())); } else { mTargetPanel.setVisibility(View.GONE); } } /** * @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()); ViewGroup.LayoutParams params = mTalkView.getLayoutParams(); params.height = settings.getPTTButtonHeight(); mTalkButton.setLayoutParams(params); boolean showPttButton = settings.isPushToTalkButtonShown() && settings.getInputMethod().equals(Settings.ARRAY_INPUT_METHOD_PTT); mTalkView.setVisibility(showPttButton ? View.VISIBLE : View.GONE); } @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_BUTTON_HEIGHT.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; } } }