Advertisement
Guest User

paint Screen

a guest
Feb 19th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.49 KB | None | 0 0
  1. package com.example.sketchapk;
  2.  
  3. import android.content.Context;
  4. import android.graphics.drawable.AdaptiveIconDrawable;
  5. import android.net.Uri;
  6. import android.os.Bundle;
  7. import android.os.CountDownTimer;
  8. import android.os.Handler;
  9. import android.support.v4.app.Fragment;
  10. import android.support.v4.app.FragmentManager;
  11. import android.support.v4.app.FragmentTransaction;
  12. import android.view.LayoutInflater;
  13. import android.view.View;
  14. import android.view.ViewGroup;
  15. import android.widget.Button;
  16. import android.widget.TextView;
  17.  
  18. import java.util.Locale;
  19.  
  20. import androidx.navigation.Navigation;
  21. import androidx.navigation.fragment.NavHostFragment;
  22.  
  23.  
  24. /**
  25. * A simple {@link Fragment} subclass.
  26. * Activities that contain this fragment must implement the
  27. * {@link PaintScreen.OnFragmentInteractionListener} interface
  28. * to handle interaction events.
  29. * Use the {@link PaintScreen#newInstance} factory method to
  30. * create an instance of this fragment.
  31. */
  32. public class PaintScreen extends Fragment {
  33.  
  34. private TextView diffBund;
  35. private Button buttonStartPause;
  36. private Button done;
  37. private CountDownTimer countDownTimer;
  38. private boolean timerRunning;
  39. private long timeLeftInMillis;
  40. private long endTime;
  41.  
  42. // TODO: Rename parameter arguments, choose names that match
  43. // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
  44. private static final String ARG_PARAM1 = "param1";
  45. private static final String ARG_PARAM2 = "param2";
  46.  
  47. // TODO: Rename and change types of parameters
  48. private String mParam1;
  49. private String mParam2;
  50.  
  51. private OnFragmentInteractionListener mListener;
  52.  
  53. public PaintScreen() {
  54. // Required empty public constructor
  55. }
  56.  
  57. /**
  58. * Use this factory method to create a new instance of
  59. * this fragment using the provided parameters.
  60. *
  61. * @param param1 Parameter 1.
  62. * @param param2 Parameter 2.
  63. * @return A new instance of fragment PaintScreen.
  64. */
  65. // TODO: Rename and change types and number of parameters
  66. public static PaintScreen newInstance(String param1, String param2) {
  67. PaintScreen fragment = new PaintScreen();
  68. Bundle args = new Bundle();
  69. args.putString(ARG_PARAM1, param1);
  70. args.putString(ARG_PARAM2, param2);
  71. fragment.setArguments(args);
  72. return fragment;
  73. }
  74. @Override
  75. public void onCreate(Bundle savedInstanceState) {
  76. super.onCreate(savedInstanceState);
  77. if (getArguments() != null) {
  78. mParam1 = getArguments().getString(ARG_PARAM1);
  79. mParam2 = getArguments().getString(ARG_PARAM2);
  80. }
  81. }
  82. @Override
  83. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  84. // Inflate the layout for this fragment
  85. View view = inflater.inflate(R.layout.fragment_paint_screen, container, false);
  86.  
  87. buttonStartPause = view.findViewById(R.id.button_start_pause);
  88. done = view.findViewById(R.id.buttonDone);
  89. /*buttonStartPause.setOnClickListener(new View.OnClickListener() {
  90. @Override
  91. public void onClick(View v) {
  92. if (timerRunning) {
  93. pauseTimer();
  94. } else {
  95. startTimer();
  96. }
  97. }
  98. });*/
  99.  
  100. return view;
  101. }
  102. // TODO: Rename method, update argument and hook method into UI event
  103. public void onButtonPressed(Uri uri) {
  104. if (mListener != null) {
  105. mListener.onFragmentInteraction(uri);
  106. }
  107. }
  108.  
  109. @Override
  110. public void onAttach(Context context) {
  111. super.onAttach(context);
  112. if (context instanceof OnFragmentInteractionListener) {
  113. mListener = (OnFragmentInteractionListener) context;
  114. } else {
  115. throw new RuntimeException(context.toString()
  116. + " must implement OnFragmentInteractionListener");
  117. }
  118. }
  119.  
  120. @Override
  121. public void onDetach() {
  122. super.onDetach();
  123. mListener = null;
  124. }
  125.  
  126. /**
  127. * This interface must be implemented by activities that contain this
  128. * fragment to allow an interaction in this fragment to be communicated
  129. * to the activity and potentially other fragments contained in that
  130. * activity.
  131. * <p>
  132. * See the Android Training lesson <a href=
  133. * "http://developer.android.com/training/basics/fragments/communicating.html"
  134. * >Communicating with Other Fragments</a> for more information.
  135. */
  136. public interface OnFragmentInteractionListener {
  137. // TODO: Update argument type and name
  138. void onFragmentInteraction(Uri uri);
  139. }
  140. public void onActivityCreated(Bundle savedInstanceState)
  141. {
  142. super.onActivityCreated(savedInstanceState);
  143. Bundle b2 = getArguments();
  144. diffBund = getView().findViewById(R.id.text_view_countdown);
  145. String time = b2.getString("time");
  146. Long timer = Long.parseLong(time);
  147. if(timer != null)
  148. {
  149. String initial_Time = timer.toString();
  150. diffBund.setText(initial_Time);
  151. timeLeftInMillis = timer;
  152. if(timerRunning == false)
  153. {
  154. buttonStartPause.setVisibility(View.INVISIBLE);
  155. startTimer();
  156. updateCountDownText();
  157. }
  158.  
  159. }
  160. Button button1 = getView().findViewById(R.id.buttonDone);
  161. button1.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.toEnd, null));
  162. }
  163.  
  164.  
  165. private void startTimer() {
  166. endTime = System.currentTimeMillis() + timeLeftInMillis;
  167.  
  168. countDownTimer = new CountDownTimer(timeLeftInMillis, 1000) {
  169. @Override
  170. public void onTick(long millisUntilFinished) {
  171. timeLeftInMillis = millisUntilFinished;
  172. updateCountDownText();
  173. }
  174.  
  175. @Override
  176. public void onFinish() {
  177. timerRunning = false;
  178. updateButtons();
  179. }
  180. }.start();
  181.  
  182. timerRunning = true;
  183. //updateButtons();
  184. }
  185. /*private void pauseTimer() {
  186. countDownTimer.cancel();
  187. timerRunning = false;
  188. updateButtons();
  189. }*/
  190. private void updateCountDownText()
  191. {
  192. int minutes = (int) (timeLeftInMillis / 1000) / 60;
  193. int seconds = (int) (timeLeftInMillis / 1000) % 60;
  194. String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);
  195. diffBund.setText(timeLeftFormatted);
  196. }
  197. private void updateButtons() {
  198. if (timerRunning) {
  199. // buttonReset.setVisibility(View.INVISIBLE);
  200. //buttonStartPause.setText("Pause");
  201. } else {
  202. //buttonStartPause.setText("Start");
  203.  
  204. if (timeLeftInMillis < 1000)
  205. {
  206. buttonStartPause.setVisibility(View.INVISIBLE);
  207. diffBund.setVisibility(View.INVISIBLE);
  208. done.setVisibility(View.INVISIBLE);
  209. NavHostFragment.findNavController(this).navigate(R.id.toEnd);
  210. //diffBund.setText("Time's up");
  211. /* Navigation.findNavController(arg0).navigate(R.id.toEnd);*/
  212. //endScreen es = new endScreen();
  213. // getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.catToPaint, new endScreen()).commit();
  214. }
  215. }
  216. }
  217.  
  218.  
  219.  
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement