Java tutorial
/* * Copyright 2016 Edward Eisenhart * * 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 eisene.riskspeedtools; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.os.Vibrator; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.text.InputType; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import java.util.ArrayList; import eisene.riskspeedtools.ColorSelectorActivity.Color; /** * Created by Edward Eisenhart on Jan 2017. */ public class TimerSetupFrag extends Fragment implements TimerSetupView { public static final String BUTTON_ID_KEY = "BUTTON_ID"; private static final int COLOR_REQUEST_CODE = 227; private static final int NUM_PLAYERS_REQUEST_CODE = 179; private TimerSetupPresenter mPresenter; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_timer_setup, container, false); } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); mPresenter = new TimerSetupPresenterImpl(this); setupOrderOnClickListeners(); timerPausePresentation(); updatePlayerOrder(mPresenter.getPlayerOrder()); updateNumPlayers(6); } @Override public int getTimerSeconds() { EditText secondET = (EditText) findViewById(R.id.et_timer_second); return Converters.getInt(secondET); } @Override public void setTimerSeconds(int seconds) { EditText secondET = (EditText) findViewById(R.id.et_timer_second); secondET.setText(Integer.toString(seconds)); } @Override public void onTimerFinished() { Vibrator v = (Vibrator) getActivity().getSystemService(Context.VIBRATOR_SERVICE); v.vibrate(1500); } public void pauseStartOnClick() { mPresenter.startPauseOnClick(); } public void resetOnClick() { mPresenter.resetOnClick(true); } public void timerPausePresentation() { EditText secondET = (EditText) findViewById(R.id.et_timer_second); Button startButton = (Button) findViewById(R.id.btn_timer_play_pause); secondET.setEnabled(true); secondET.setInputType(InputType.TYPE_CLASS_NUMBER); startButton.setText(R.string.timer_play); } public void timerStartPresentation() { EditText secondET = (EditText) findViewById(R.id.et_timer_second); Button startButton = (Button) findViewById(R.id.btn_timer_play_pause); secondET.setEnabled(false); // prevent edit while running secondET.setInputType(InputType.TYPE_NULL); startButton.setText(R.string.timer_pause); } public void nextPlayerOnClick() { mPresenter.nextPlayerOnClick(); } public void previousPlayerOnClick() { mPresenter.previousPlayerOnClick(); } public void onColorClick(View v) { Intent i = new Intent(getActivity(), ColorSelectorActivity.class); i.putExtra(BUTTON_ID_KEY, v.getId()); startActivityForResult(i, COLOR_REQUEST_CODE); } public void numPlayerOnClick() { Intent i = new Intent(getActivity(), NumPlayerSelectorActivity.class); startActivityForResult(i, NUM_PLAYERS_REQUEST_CODE); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == COLOR_REQUEST_CODE) { if (resultCode == Activity.RESULT_OK) { Color newColor = (Color) data.getSerializableExtra(ColorSelectorActivity.RESULT_ID_KEY); int player = Converters.buttonIdToPlayer(data.getIntExtra(BUTTON_ID_KEY, 1)); mPresenter.updatePlayerColor(player, newColor); } } else if (requestCode == NUM_PLAYERS_REQUEST_CODE) { if (resultCode == Activity.RESULT_OK) { int numPlayers = data.getIntExtra(NumPlayerSelectorActivity.RESULT_ID_KEY, 1); mPresenter.setNumberOfPlayers(numPlayers); } } } /** * Updates the player order on the view. * @param playerOrder The new player order. */ public void updatePlayerOrder(ArrayList<Color> playerOrder) { Button one = (Button) findViewById(R.id.color_selector_p1); Button two = (Button) findViewById(R.id.color_selector_p2); Button three = (Button) findViewById(R.id.color_selector_p3); Button four = (Button) findViewById(R.id.color_selector_p4); Button five = (Button) findViewById(R.id.color_selector_p5); Button six = (Button) findViewById(R.id.color_selector_p6); setButtonColor(one, playerOrder.get(0)); setButtonColor(two, playerOrder.get(1)); setButtonColor(three, playerOrder.get(2)); setButtonColor(four, playerOrder.get(3)); setButtonColor(five, playerOrder.get(4)); setButtonColor(six, playerOrder.get(5)); updateLRColors(); } /** * Update the number of players displayed on the view. * @param numPlayers The number of players. */ public void updateNumPlayers(int numPlayers) { Button numPlayer = (Button) findViewById(R.id.num_player_selector_btn); numPlayer.setText(String.valueOf(numPlayers)); hideUnusedPlayers(numPlayers); updateLRColors(); } private void setButtonColor(Button b, Color color) { int backgroundColor = Converters.getColor(color); b.setBackgroundColor(backgroundColor); if (backgroundColor == android.graphics.Color.BLACK) { b.setTextColor(android.graphics.Color.WHITE); } else { b.setTextColor(android.graphics.Color.BLACK); } } /** * Updates the next and previous colors. */ private void updateLRColors() { Button next = (Button) findViewById(R.id.btn_next_player); Button prev = (Button) findViewById(R.id.btn_prev_player); Color prevC = mPresenter.getPrevPlayer(); Color nextC = mPresenter.getNextPlayer(); next.setBackgroundColor(Converters.getColor(nextC)); prev.setBackgroundColor(Converters.getColor(prevC)); } /** * Hides unused players in the player order depending on then number of players. * @param numPlayers The number of current players */ private void hideUnusedPlayers(int numPlayers) { Button one = (Button) findViewById(R.id.color_selector_p1); Button two = (Button) findViewById(R.id.color_selector_p2); Button three = (Button) findViewById(R.id.color_selector_p3); Button four = (Button) findViewById(R.id.color_selector_p4); Button five = (Button) findViewById(R.id.color_selector_p5); Button six = (Button) findViewById(R.id.color_selector_p6); int twoVisiblility = (numPlayers > 1) ? View.VISIBLE : View.INVISIBLE; int threeVisiblility = (numPlayers > 2) ? View.VISIBLE : View.INVISIBLE; int fourVisiblility = (numPlayers > 3) ? View.VISIBLE : View.INVISIBLE; int fiveVisiblility = (numPlayers > 4) ? View.VISIBLE : View.INVISIBLE; int sixVisiblility = (numPlayers > 5) ? View.VISIBLE : View.INVISIBLE; one.setVisibility(View.VISIBLE); two.setVisibility(twoVisiblility); three.setVisibility(threeVisiblility); four.setVisibility(fourVisiblility); five.setVisibility(fiveVisiblility); six.setVisibility(sixVisiblility); } /** * Look for a child view with the given id. If this view has the given * id, return this view. * * @param id The id to search for. * @return The view that has the given id in the hierarchy or null */ private View findViewById(int id) { return getView().findViewById(id); } /** * Sets up the player order on click listeners */ private void setupOrderOnClickListeners() { (findViewById(R.id.color_selector_p1)).setOnClickListener(mPlayerOrderButtonClick); (findViewById(R.id.color_selector_p2)).setOnClickListener(mPlayerOrderButtonClick); (findViewById(R.id.color_selector_p3)).setOnClickListener(mPlayerOrderButtonClick); (findViewById(R.id.color_selector_p4)).setOnClickListener(mPlayerOrderButtonClick); (findViewById(R.id.color_selector_p5)).setOnClickListener(mPlayerOrderButtonClick); (findViewById(R.id.color_selector_p6)).setOnClickListener(mPlayerOrderButtonClick); (findViewById(R.id.num_player_selector_btn)).setOnClickListener(mNumPlayerButtonClick); (findViewById(R.id.btn_timer_play_pause)).setOnClickListener(mPauseStartButtonClick); (findViewById(R.id.btn_next_player)).setOnClickListener(mNextPlayerClick); (findViewById(R.id.btn_prev_player)).setOnClickListener(mPreviousPlayerClick); (findViewById(R.id.btn_timer_reset)).setOnClickListener(mResetClick); } private View.OnClickListener mPlayerOrderButtonClick = new View.OnClickListener() { public void onClick(View v) { onColorClick(v); } }; private View.OnClickListener mNumPlayerButtonClick = new View.OnClickListener() { public void onClick(View v) { numPlayerOnClick(); } }; private View.OnClickListener mPauseStartButtonClick = new View.OnClickListener() { public void onClick(View v) { pauseStartOnClick(); } }; private View.OnClickListener mNextPlayerClick = new View.OnClickListener() { public void onClick(View v) { nextPlayerOnClick(); } }; private View.OnClickListener mPreviousPlayerClick = new View.OnClickListener() { public void onClick(View v) { previousPlayerOnClick(); } }; private View.OnClickListener mResetClick = new View.OnClickListener() { public void onClick(View v) { resetOnClick(); } }; }