Guest User

Untitled

a guest
Oct 21st, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.73 KB | None | 0 0
  1. public class TestFinalViewActivity extends Activity implements
  2.         ViewPager.OnPageChangeListener,
  3.         SharedPreferences.OnSharedPreferenceChangeListener,
  4.         View.OnClickListener {
  5.     private static final String LAPS_LOADING_ERROR = "Loading failed!";
  6.  
  7.     private Activity mContext;
  8.     private ViewPager mViewPager;
  9.     private SharedPreferences mSharedPreferences, mDefaultSharedPreferences;
  10.     private boolean mAnimationEnabled, mAnimationScrollingEnabled,
  11.             HoneyCombSupported;
  12.  
  13.     // ActionBar
  14.     private LinearLayout mActionBarSwitchBtn;
  15.     private TextView mActionBarTitle;
  16.     private ImageView mActionBarSettings, mActionBarAddTimer;
  17.  
  18.     // Timer values
  19.     private View mTimerView;
  20.     private Button mTimerStartButton, mTimerResetButton;
  21.     private TimeBar mTimerTimeTextView;
  22.     private ListView mTimerListView;
  23.  
  24.     // Stopwatch values
  25.     private Button mStopwatchStartButton, mStopwatchResetButton;
  26.     private TimeBar mStopwatchTimeTextView;
  27.     private ListView mStopwatchListView;
  28.     private View mStopwatchView;
  29.  
  30.     private long mStopwatchStartTime, mStopwatchPauseTime, mStopwatchFullTime,
  31.             mStopwatchLapTime;
  32.     private ArrayList<String> mStopwatchPrimaryLine, mStopwatchSecondaryLine;
  33.     private boolean mStopwatchTimeTextMode, mStopwatchRunning;
  34.  
  35.     static class Key {
  36.         public final static String KEY_SHARED_PREFERENCES_STOPWATCH = "Stopwatch";
  37.  
  38.         public final static String KEY_SHARED_PREFERENCES_STOPWATCH_START_TIME = "sst";
  39.         public final static String KEY_SHARED_PREFERENCES_STOPWATCH_PAUSE_TIME = "spt";
  40.         public final static String KEY_SHARED_PREFERENCES_STOPWATCH_FULL_TIME = "sft";
  41.         public final static String KEY_SHARED_PREFERENCES_STOPWATCH_LAPS_LENGTH = "sll";
  42.         public final static String KEY_SHARED_PREFERENCES_STOPWATCH_PRIMARY_LINE = "spli=";
  43.         public final static String KEY_SHARED_PREFERENCES_STOPWATCH_SECONDARY_LINE = "ssli=";
  44.  
  45.         public final static String KEY_SHARED_PREFERENCES_TIMER = "Timer";
  46.         public final static String KEY_SHARED_PREFERENCES_TIMER_LENGTH = "tl";
  47.         public final static String KEY_SHARED_PREFERENCES_TIMER_PRIMARY_LINE = "tpli=";
  48.         public final static String KEY_SHARED_PREFERENCES_TIMER_SECONDARY_LINE = "tsli=";
  49.  
  50.         public final static String KEY_SHARED_PREFERENCES_MAIN = "Main";
  51.         public final static String KEY_SHARED_PREFERENCES_MAIN_CURRENT_PAGE = "mcp";
  52.     }
  53.  
  54.     @Override
  55.     protected void onCreate(Bundle savedInstanceState) {
  56.         super.onCreate(savedInstanceState);
  57.         mContext = this;
  58.         HoneyCombSupported = Utils.isHcDevice();
  59.         mSharedPreferences = getSharedPreferences(
  60.                 Key.KEY_SHARED_PREFERENCES_STOPWATCH, 0);
  61.         mDefaultSharedPreferences = PreferenceManager
  62.                 .getDefaultSharedPreferences(mContext);
  63.         mDefaultSharedPreferences
  64.                 .registerOnSharedPreferenceChangeListener(this);
  65.  
  66.         updateStopwatchTimeTextModeValue(mDefaultSharedPreferences);
  67.         updateGeneralAnimationEnabledValues(mDefaultSharedPreferences);
  68.         updateGeneralAnimationScrollingEnabledValues(mDefaultSharedPreferences);
  69.  
  70.         LayoutInflater inflater = LayoutInflater.from(this);
  71.         List<View> pages = new ArrayList<View>();
  72.  
  73.         // ActionBar, main
  74.         final View viewTimekeeper = inflater.inflate(R.layout.timekeeper, null);
  75.         mActionBarTitle = (TextView) viewTimekeeper.findViewById(R.id.title);
  76.         mActionBarSettings = (ImageView) viewTimekeeper
  77.                 .findViewById(R.id.settings);
  78.         mActionBarAddTimer = (ImageView) viewTimekeeper
  79.                 .findViewById(R.id.custom_view);
  80.         mActionBarSwitchBtn = (LinearLayout) viewTimekeeper
  81.                 .findViewById(R.id.switch_btn);
  82.  
  83.         // Stopwatch
  84.         mStopwatchView = inflater.inflate(R.layout.test, null);
  85.         mStopwatchStartButton = (Button) mStopwatchView
  86.                 .findViewById(R.id.start);
  87.         mStopwatchResetButton = (Button) mStopwatchView
  88.                 .findViewById(R.id.clear);
  89.         mStopwatchTimeTextView = (TimeBar) mStopwatchView
  90.                 .findViewById(R.id.time_bar);
  91.         mStopwatchListView = (ListView) mStopwatchView.findViewById(R.id.list);
  92.         pages.add(mStopwatchView);
  93.  
  94.         // Timer
  95.         mTimerView = inflater.inflate(R.layout.test, null);
  96.         mTimerStartButton = (Button) mStopwatchView.findViewById(R.id.start);
  97.         mTimerResetButton = (Button) mStopwatchView.findViewById(R.id.clear);
  98.         mTimerTimeTextView = (TimeBar) mStopwatchView
  99.                 .findViewById(R.id.time_bar);
  100.         mTimerListView = (ListView) mStopwatchView.findViewById(R.id.list);
  101.         pages.add(mTimerView);
  102.  
  103.         mViewPager = (ViewPager) viewTimekeeper.findViewById(R.id.view_pager);
  104.         mViewPager.setAdapter(new TestViewPagerAdapter(pages));
  105.         mViewPager.setPageMarginDrawable(R.drawable.transparency);
  106.  
  107.         switch (getSharedPreferences(Key.KEY_SHARED_PREFERENCES_MAIN, 0)
  108.                 .getInt(Key.KEY_SHARED_PREFERENCES_MAIN_CURRENT_PAGE, 0)) {
  109.         case 0:
  110.             mViewPager.setCurrentItem(0);
  111.             mActionBarAddTimer.setImageResource(R.drawable.transparency);
  112.             mActionBarAddTimer.setClickable(false);
  113.             mActionBarTitle.setText("Секундомер");
  114.             break;
  115.         case 1:
  116.             mViewPager.setCurrentItem(1);
  117.             mActionBarAddTimer
  118.                     .setImageResource(R.drawable.ic_actionbar_menu_add);
  119.             mActionBarAddTimer.setClickable(true);
  120.             mActionBarTitle.setText("Таймер");
  121.             break;
  122.         }
  123.  
  124.         updateGeneralPageMarginEnabledValues(mDefaultSharedPreferences);
  125.  
  126.         // setContentView(new NumberPicker(this));
  127.         setContentView(viewTimekeeper);
  128.     }
  129.  
  130.     @Override
  131.     public void onResume() {
  132.         super.onResume();
  133.         getDataFromSharedPreferences();
  134.  
  135.         // Attach listeners
  136.         mViewPager.setOnPageChangeListener(this);
  137.         mStopwatchStartButton.setOnClickListener(this);
  138.         mStopwatchResetButton.setOnClickListener(this);
  139.         mActionBarSettings.setOnClickListener(this);
  140.         mActionBarSwitchBtn.setOnClickListener(this);
  141.  
  142.         mActionBarAddTimer.setOnClickListener(this);
  143.         if (mViewPager.getCurrentItem() == 0)
  144.             mActionBarAddTimer.setClickable(false);
  145.     }
  146.  
  147.     @Override
  148.     public void onPause() {
  149.         super.onPause();
  150.  
  151.         // Hide TimeBar handler
  152.         mStopwatchRunning = false;
  153.  
  154.         // Detach active listeners
  155.         mViewPager.setOnPageChangeListener(null);
  156.         mStopwatchStartButton.setOnClickListener(null);
  157.         mStopwatchResetButton.setOnClickListener(null);
  158.         mActionBarAddTimer.setOnClickListener(null);
  159.         mActionBarSettings.setOnClickListener(null);
  160.         mActionBarSwitchBtn.setOnClickListener(null);
  161.  
  162.         // Save data
  163.         final SharedPreferences.Editor editor = mSharedPreferences.edit();
  164.         editor.putLong(Key.KEY_SHARED_PREFERENCES_STOPWATCH_START_TIME,
  165.                 mStopwatchStartTime);
  166.         editor.putInt(Key.KEY_SHARED_PREFERENCES_STOPWATCH_LAPS_LENGTH,
  167.                 mStopwatchPrimaryLine.size());
  168.         editor.putLong(Key.KEY_SHARED_PREFERENCES_STOPWATCH_FULL_TIME,
  169.                 mStopwatchFullTime);
  170.         editor.putLong(Key.KEY_SHARED_PREFERENCES_STOPWATCH_PAUSE_TIME,
  171.                 mStopwatchPauseTime).commit();
  172.     }
  173.  
  174.     @Override
  175.     public void onDestroy() {
  176.         super.onDestroy();
  177.         getSharedPreferences(Key.KEY_SHARED_PREFERENCES_MAIN, 0)
  178.                 .edit()
  179.                 .putInt(Key.KEY_SHARED_PREFERENCES_MAIN_CURRENT_PAGE,
  180.                         mViewPager.getCurrentItem()).commit();
  181.         mDefaultSharedPreferences
  182.                 .unregisterOnSharedPreferenceChangeListener(this);
  183.     }
  184.  
  185.     @Override
  186.     public void onPageScrolled(int position, float positionOffset,
  187.             int positionOffsetPixels) {
  188.         // TODO: Add gingerbread animation
  189.         if (HoneyCombSupported && mAnimationEnabled
  190.                 && mAnimationScrollingEnabled) {
  191.             mStopwatchView.setRotationY(positionOffset * 45);
  192.             mStopwatchView.setAlpha(1 - positionOffset / 2);
  193.             if (positionOffset == 0)
  194.                 positionOffset = 1;
  195.             mTimerView.setRotationY(45 - positionOffset * 45);
  196.             mTimerView.setAlpha((float) (positionOffset / 2 + 0.5));
  197.         }
  198.  
  199.     }
  200.  
  201.     @Override
  202.     public void onPageSelected(int position) {
  203.         switch (position) {
  204.         case 0:
  205.             mActionBarAddTimer.setImageResource(R.drawable.transparency);
  206.             mActionBarAddTimer.setClickable(false);
  207.             mActionBarTitle.setText("Секундомер");
  208.             if (getCurrentState() == 0)
  209.                 run();
  210.             break;
  211.         case 1:
  212.             mActionBarAddTimer
  213.                     .setImageResource(R.drawable.ic_actionbar_menu_add);
  214.             mActionBarAddTimer.setClickable(true);
  215.             mActionBarTitle.setText("Таймер");
  216.             mStopwatchRunning = false;
  217.             break;
  218.         }
  219.     }
  220.  
  221.     @Override
  222.     public void onPageScrollStateChanged(int state) {
  223.  
  224.     }
  225.  
  226.     @Override
  227.     public void onClick(View view) {
  228.         if (view == mActionBarAddTimer) {
  229.             startActivity(new Intent(mContext, TestAddNewTimer.class));
  230.         } else if (view == mActionBarSwitchBtn) {
  231.             mViewPager.setCurrentItem((mViewPager.getCurrentItem() - 1) * -1);
  232.         } else if (view == mActionBarSettings) {
  233.             if (HoneyCombSupported)
  234.                 startActivity(new Intent(mContext, Settings.class));
  235.             else {
  236.                 // TODO: Add gingerbread settings
  237.             }
  238.         } else
  239.             switch (getCurrentState()) {
  240.             case 0:
  241.                 if (view == mStopwatchStartButton) {
  242.                     // Pause stopwatch
  243.                     mStopwatchRunning = false;
  244.  
  245.                     mStopwatchPauseTime = System.currentTimeMillis()
  246.                             - mStopwatchStartTime;
  247.                     mStopwatchStartTime = 0;
  248.  
  249.                     mStopwatchTimeTextView
  250.                             .setTextByTime(mStopwatchTimeTextMode ? mStopwatchPauseTime
  251.                                     : mStopwatchPauseTime + mStopwatchFullTime);
  252.                     setButtonsTextByPaused();
  253.                 } else if (view == mStopwatchResetButton) {
  254.                     final String primaryLine = new String(
  255.                             Utils.convertTimeToStringHhMmSsMs(mStopwatchLapTime));
  256.                     final String secondaryLine = new String(
  257.                             Utils.convertTimeToStringHhMmSsMs(mStopwatchFullTime += mStopwatchLapTime));
  258.  
  259.                     mStopwatchStartTime = System.currentTimeMillis();
  260.  
  261.                     mStopwatchPrimaryLine.add(primaryLine);
  262.                     mStopwatchSecondaryLine.add(secondaryLine);
  263.                     updateListView();
  264.  
  265.                     final int length = mStopwatchPrimaryLine.size();
  266.                     final SharedPreferences.Editor editor = mSharedPreferences
  267.                             .edit();
  268.                     editor.putString(
  269.                             Key.KEY_SHARED_PREFERENCES_STOPWATCH_PRIMARY_LINE
  270.                                     + (length - 1), primaryLine);
  271.                     editor.putString(
  272.                             Key.KEY_SHARED_PREFERENCES_STOPWATCH_SECONDARY_LINE
  273.                                     + (length - 1), secondaryLine).commit();
  274.                 }
  275.                 break;
  276.             default:
  277.                 if (view == mStopwatchStartButton) {
  278.                     // Start stopwatch
  279.                     mStopwatchStartTime = System.currentTimeMillis()
  280.                             - mStopwatchPauseTime;
  281.                     mStopwatchPauseTime = 0;
  282.                     setButtonsTextByRunning();
  283.  
  284.                     run();
  285.                 } else if (view == mStopwatchResetButton) {
  286.                     // Reset stopwatch
  287.                     mStopwatchRunning = false;
  288.  
  289.                     mStopwatchPauseTime = 0;
  290.                     mStopwatchStartTime = 0;
  291.                     mStopwatchFullTime = 0;
  292.  
  293.                     mStopwatchPrimaryLine = new ArrayList<String>();
  294.                     mStopwatchSecondaryLine = new ArrayList<String>();
  295.  
  296.                     updateListView();
  297.                     setTimeTextToZero();
  298.  
  299.                     mSharedPreferences.edit().clear().apply();
  300.                 }
  301.                 break;
  302.             }
  303.     }
  304.  
  305.     @Override
  306.     public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
  307.             String key) {
  308.         if (key.equals(Settings.Key.KEY_STOPWATCH_MORE_TIMEMODE))
  309.             updateStopwatchTimeTextModeValue(sharedPreferences);
  310.         else if (key.equals(Settings.Key.KEY_GENERAL_ANIMATION_ENABLED)) {
  311.             updateGeneralAnimationEnabledValues(sharedPreferences);
  312.             if (!mAnimationEnabled && mAnimationScrollingEnabled
  313.                     && HoneyCombSupported)
  314.                 resetScrollingAnimation();
  315.         } else if (key
  316.                 .equals(Settings.Key.KEY_GENERAL_ANIMATION_SCROLLING_ENABLED)) {
  317.             updateGeneralAnimationScrollingEnabledValues(sharedPreferences);
  318.             if (!mAnimationScrollingEnabled && HoneyCombSupported)
  319.                 resetScrollingAnimation();
  320.  
  321.         } else if (key.equals(Settings.Key.KEY_GENERAL_PAGE_MARGIN_ENABLED)) {
  322.             updateGeneralPageMarginEnabledValues(sharedPreferences);
  323.         }
  324.     }
  325.  
  326.     private void resetScrollingAnimation() {
  327.         mStopwatchView.setRotationY(0);
  328.         mTimerView.setRotationY(0);
  329.         mStopwatchView.setAlpha(1);
  330.         mTimerView.setAlpha(1);
  331.     }
  332.  
  333.     private void updateStopwatchTimeTextModeValue(SharedPreferences prefs) {
  334.         mStopwatchTimeTextMode = prefs.getBoolean(
  335.                 Settings.Key.KEY_STOPWATCH_MORE_TIMEMODE, getResources()
  336.                         .getBoolean(R.bool.settings_stopwatch_more_timemode));
  337.  
  338.     }
  339.  
  340.     private void updateGeneralAnimationEnabledValues(SharedPreferences prefs) {
  341.         mAnimationEnabled = prefs.getBoolean(
  342.                 Settings.Key.KEY_GENERAL_ANIMATION_ENABLED, getResources()
  343.                         .getBoolean(R.bool.settings_general_animation_enabled));
  344.     }
  345.  
  346.     private void updateGeneralAnimationScrollingEnabledValues(
  347.             SharedPreferences prefs) {
  348.         mAnimationScrollingEnabled = prefs.getBoolean(
  349.                 Settings.Key.KEY_GENERAL_ANIMATION_SCROLLING_ENABLED,
  350.                 getResources().getBoolean(
  351.                         R.bool.settings_general_animation_scrolling_enabled));
  352.     }
  353.  
  354.     private void updateGeneralPageMarginEnabledValues(SharedPreferences prefs) {
  355.         mViewPager
  356.                 .setPageMargin(prefs
  357.                         .getBoolean(
  358.  
  359.                                 Settings.Key.KEY_GENERAL_PAGE_MARGIN_ENABLED,
  360.                                 getResources()
  361.                                         .getBoolean(
  362.                                                 R.bool.settings_general_interface_page_margin_enabled)) ? 10
  363.                         : 0);
  364.  
  365.     }
  366.  
  367.     private void run() {
  368.         if (!mStopwatchRunning) {
  369.             mStopwatchRunning = true;
  370.             mRunnerHandler.post(mRunnerRunnable);
  371.         }
  372.     }
  373.  
  374.     private final Handler mRunnerHandler = new Handler();
  375.     private final Runnable mRunnerRunnable = new Runnable() {
  376.  
  377.         private final static int MAX_FPS = 16666666, MIN_FPS = 33333333;
  378.  
  379.         private long last = System.nanoTime();
  380.         private int sleepTime = 0;
  381.  
  382.         @Override
  383.         public void run() {
  384.             if (mStopwatchRunning) {
  385.                 final long now = System.nanoTime();
  386.                 final long fps = now - last;
  387.                 if (fps < MAX_FPS)
  388.                     sleepTime++;
  389.                 else if (fps > MIN_FPS && sleepTime > 0)
  390.                     sleepTime--;
  391.                 mStopwatchLapTime = System.currentTimeMillis()
  392.                         - mStopwatchStartTime;
  393.                 mStopwatchTimeTextView
  394.                         .setTextByTime(mStopwatchTimeTextMode ? mStopwatchLapTime
  395.                                 : mStopwatchLapTime + mStopwatchFullTime);
  396.                 last = now;
  397.                 mRunnerHandler.postDelayed(mRunnerRunnable, sleepTime);
  398.             }
  399.         }
  400.     };
  401.  
  402.     private int getCurrentState() {
  403.         return mStopwatchStartTime != 0 ? 0 : mStopwatchPauseTime != 0 ? 1 : 2;
  404.     }
  405.  
  406.     public void startByUsingData() {
  407.         switch (getCurrentState()) {
  408.         case 0:
  409.             setButtonsTextByRunning();
  410.             run();
  411.             break;
  412.         case 1:
  413.             mStopwatchTimeTextView.setTextByTime(mStopwatchPauseTime);
  414.             setButtonsTextByPaused();
  415.             break;
  416.         case 2:
  417.             setTimeTextToZero();
  418.             break;
  419.         }
  420.     }
  421.  
  422.     private void setTimeTextToZero() {
  423.         mStopwatchTimeTextView.setTextByTime(0);
  424.         setButtonsTextByPaused();
  425.     }
  426.  
  427.     private void setButtonsTextByPaused() {
  428.         mStopwatchStartButton.setText(getString(R.string.stopwatch_start));
  429.         mStopwatchResetButton.setText(getString(R.string.stopwatch_reset));
  430.     }
  431.  
  432.     private void setButtonsTextByRunning() {
  433.         mStopwatchStartButton.setText(getString(R.string.stopwatch_pause));
  434.         mStopwatchResetButton.setText(getString(R.string.stopwatch_lap));
  435.     }
  436.  
  437.     private void getDataFromSharedPreferences() {
  438.         mStopwatchStartTime = mSharedPreferences.getLong(
  439.                 Key.KEY_SHARED_PREFERENCES_STOPWATCH_START_TIME, 0);
  440.         mStopwatchPauseTime = mSharedPreferences.getLong(
  441.                 Key.KEY_SHARED_PREFERENCES_STOPWATCH_PAUSE_TIME, 0);
  442.         mStopwatchFullTime = mSharedPreferences.getLong(
  443.                 Key.KEY_SHARED_PREFERENCES_STOPWATCH_FULL_TIME, 0);
  444.         int length = mSharedPreferences.getInt(
  445.                 Key.KEY_SHARED_PREFERENCES_STOPWATCH_LAPS_LENGTH, 0);
  446.         if (mStopwatchPrimaryLine == null) {
  447.             mStopwatchPrimaryLine = new ArrayList<String>();
  448.             mStopwatchSecondaryLine = new ArrayList<String>();
  449.         }
  450.         for (int i = mStopwatchPrimaryLine.size(); i < length; i++) {
  451.             mStopwatchPrimaryLine.add(new String(mSharedPreferences.getString(
  452.                     Key.KEY_SHARED_PREFERENCES_STOPWATCH_PRIMARY_LINE + i,
  453.                     LAPS_LOADING_ERROR)));
  454.             mStopwatchSecondaryLine.add(new String(mSharedPreferences
  455.                     .getString(
  456.                             Key.KEY_SHARED_PREFERENCES_STOPWATCH_SECONDARY_LINE
  457.                                     + i, LAPS_LOADING_ERROR)));
  458.  
  459.         }
  460.         updateListView();
  461.         startByUsingData();
  462.     }
  463.  
  464.     private void updateListView() {
  465.         mStopwatchListView.setAdapter(mStopwatchPrimaryLine.size() == 0 ? null
  466.                 : new TestListView(mContext, mStopwatchPrimaryLine
  467.                         .toArray(new String[0]), mStopwatchSecondaryLine
  468.                         .toArray(new String[0])));
  469.     }
  470. }
Add Comment
Please, Sign In to add comment