Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TestFinalViewActivity extends Activity implements
- ViewPager.OnPageChangeListener,
- SharedPreferences.OnSharedPreferenceChangeListener,
- View.OnClickListener {
- private static final String LAPS_LOADING_ERROR = "Loading failed!";
- private Activity mContext;
- private ViewPager mViewPager;
- private SharedPreferences mSharedPreferences, mDefaultSharedPreferences;
- private boolean mAnimationEnabled, mAnimationScrollingEnabled,
- HoneyCombSupported;
- // ActionBar
- private LinearLayout mActionBarSwitchBtn;
- private TextView mActionBarTitle;
- private ImageView mActionBarSettings, mActionBarAddTimer;
- // Timer values
- private View mTimerView;
- private Button mTimerStartButton, mTimerResetButton;
- private TimeBar mTimerTimeTextView;
- private ListView mTimerListView;
- // Stopwatch values
- private Button mStopwatchStartButton, mStopwatchResetButton;
- private TimeBar mStopwatchTimeTextView;
- private ListView mStopwatchListView;
- private View mStopwatchView;
- private long mStopwatchStartTime, mStopwatchPauseTime, mStopwatchFullTime,
- mStopwatchLapTime;
- private ArrayList<String> mStopwatchPrimaryLine, mStopwatchSecondaryLine;
- private boolean mStopwatchTimeTextMode, mStopwatchRunning;
- static class Key {
- public final static String KEY_SHARED_PREFERENCES_STOPWATCH = "Stopwatch";
- public final static String KEY_SHARED_PREFERENCES_STOPWATCH_START_TIME = "sst";
- public final static String KEY_SHARED_PREFERENCES_STOPWATCH_PAUSE_TIME = "spt";
- public final static String KEY_SHARED_PREFERENCES_STOPWATCH_FULL_TIME = "sft";
- public final static String KEY_SHARED_PREFERENCES_STOPWATCH_LAPS_LENGTH = "sll";
- public final static String KEY_SHARED_PREFERENCES_STOPWATCH_PRIMARY_LINE = "spli=";
- public final static String KEY_SHARED_PREFERENCES_STOPWATCH_SECONDARY_LINE = "ssli=";
- public final static String KEY_SHARED_PREFERENCES_TIMER = "Timer";
- public final static String KEY_SHARED_PREFERENCES_TIMER_LENGTH = "tl";
- public final static String KEY_SHARED_PREFERENCES_TIMER_PRIMARY_LINE = "tpli=";
- public final static String KEY_SHARED_PREFERENCES_TIMER_SECONDARY_LINE = "tsli=";
- public final static String KEY_SHARED_PREFERENCES_MAIN = "Main";
- public final static String KEY_SHARED_PREFERENCES_MAIN_CURRENT_PAGE = "mcp";
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- mContext = this;
- HoneyCombSupported = Utils.isHcDevice();
- mSharedPreferences = getSharedPreferences(
- Key.KEY_SHARED_PREFERENCES_STOPWATCH, 0);
- mDefaultSharedPreferences = PreferenceManager
- .getDefaultSharedPreferences(mContext);
- mDefaultSharedPreferences
- .registerOnSharedPreferenceChangeListener(this);
- updateStopwatchTimeTextModeValue(mDefaultSharedPreferences);
- updateGeneralAnimationEnabledValues(mDefaultSharedPreferences);
- updateGeneralAnimationScrollingEnabledValues(mDefaultSharedPreferences);
- LayoutInflater inflater = LayoutInflater.from(this);
- List<View> pages = new ArrayList<View>();
- // ActionBar, main
- final View viewTimekeeper = inflater.inflate(R.layout.timekeeper, null);
- mActionBarTitle = (TextView) viewTimekeeper.findViewById(R.id.title);
- mActionBarSettings = (ImageView) viewTimekeeper
- .findViewById(R.id.settings);
- mActionBarAddTimer = (ImageView) viewTimekeeper
- .findViewById(R.id.custom_view);
- mActionBarSwitchBtn = (LinearLayout) viewTimekeeper
- .findViewById(R.id.switch_btn);
- // Stopwatch
- mStopwatchView = inflater.inflate(R.layout.test, null);
- mStopwatchStartButton = (Button) mStopwatchView
- .findViewById(R.id.start);
- mStopwatchResetButton = (Button) mStopwatchView
- .findViewById(R.id.clear);
- mStopwatchTimeTextView = (TimeBar) mStopwatchView
- .findViewById(R.id.time_bar);
- mStopwatchListView = (ListView) mStopwatchView.findViewById(R.id.list);
- pages.add(mStopwatchView);
- // Timer
- mTimerView = inflater.inflate(R.layout.test, null);
- mTimerStartButton = (Button) mStopwatchView.findViewById(R.id.start);
- mTimerResetButton = (Button) mStopwatchView.findViewById(R.id.clear);
- mTimerTimeTextView = (TimeBar) mStopwatchView
- .findViewById(R.id.time_bar);
- mTimerListView = (ListView) mStopwatchView.findViewById(R.id.list);
- pages.add(mTimerView);
- mViewPager = (ViewPager) viewTimekeeper.findViewById(R.id.view_pager);
- mViewPager.setAdapter(new TestViewPagerAdapter(pages));
- mViewPager.setPageMarginDrawable(R.drawable.transparency);
- switch (getSharedPreferences(Key.KEY_SHARED_PREFERENCES_MAIN, 0)
- .getInt(Key.KEY_SHARED_PREFERENCES_MAIN_CURRENT_PAGE, 0)) {
- case 0:
- mViewPager.setCurrentItem(0);
- mActionBarAddTimer.setImageResource(R.drawable.transparency);
- mActionBarAddTimer.setClickable(false);
- mActionBarTitle.setText("Секундомер");
- break;
- case 1:
- mViewPager.setCurrentItem(1);
- mActionBarAddTimer
- .setImageResource(R.drawable.ic_actionbar_menu_add);
- mActionBarAddTimer.setClickable(true);
- mActionBarTitle.setText("Таймер");
- break;
- }
- updateGeneralPageMarginEnabledValues(mDefaultSharedPreferences);
- // setContentView(new NumberPicker(this));
- setContentView(viewTimekeeper);
- }
- @Override
- public void onResume() {
- super.onResume();
- getDataFromSharedPreferences();
- // Attach listeners
- mViewPager.setOnPageChangeListener(this);
- mStopwatchStartButton.setOnClickListener(this);
- mStopwatchResetButton.setOnClickListener(this);
- mActionBarSettings.setOnClickListener(this);
- mActionBarSwitchBtn.setOnClickListener(this);
- mActionBarAddTimer.setOnClickListener(this);
- if (mViewPager.getCurrentItem() == 0)
- mActionBarAddTimer.setClickable(false);
- }
- @Override
- public void onPause() {
- super.onPause();
- // Hide TimeBar handler
- mStopwatchRunning = false;
- // Detach active listeners
- mViewPager.setOnPageChangeListener(null);
- mStopwatchStartButton.setOnClickListener(null);
- mStopwatchResetButton.setOnClickListener(null);
- mActionBarAddTimer.setOnClickListener(null);
- mActionBarSettings.setOnClickListener(null);
- mActionBarSwitchBtn.setOnClickListener(null);
- // Save data
- final SharedPreferences.Editor editor = mSharedPreferences.edit();
- editor.putLong(Key.KEY_SHARED_PREFERENCES_STOPWATCH_START_TIME,
- mStopwatchStartTime);
- editor.putInt(Key.KEY_SHARED_PREFERENCES_STOPWATCH_LAPS_LENGTH,
- mStopwatchPrimaryLine.size());
- editor.putLong(Key.KEY_SHARED_PREFERENCES_STOPWATCH_FULL_TIME,
- mStopwatchFullTime);
- editor.putLong(Key.KEY_SHARED_PREFERENCES_STOPWATCH_PAUSE_TIME,
- mStopwatchPauseTime).commit();
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- getSharedPreferences(Key.KEY_SHARED_PREFERENCES_MAIN, 0)
- .edit()
- .putInt(Key.KEY_SHARED_PREFERENCES_MAIN_CURRENT_PAGE,
- mViewPager.getCurrentItem()).commit();
- mDefaultSharedPreferences
- .unregisterOnSharedPreferenceChangeListener(this);
- }
- @Override
- public void onPageScrolled(int position, float positionOffset,
- int positionOffsetPixels) {
- // TODO: Add gingerbread animation
- if (HoneyCombSupported && mAnimationEnabled
- && mAnimationScrollingEnabled) {
- mStopwatchView.setRotationY(positionOffset * 45);
- mStopwatchView.setAlpha(1 - positionOffset / 2);
- if (positionOffset == 0)
- positionOffset = 1;
- mTimerView.setRotationY(45 - positionOffset * 45);
- mTimerView.setAlpha((float) (positionOffset / 2 + 0.5));
- }
- }
- @Override
- public void onPageSelected(int position) {
- switch (position) {
- case 0:
- mActionBarAddTimer.setImageResource(R.drawable.transparency);
- mActionBarAddTimer.setClickable(false);
- mActionBarTitle.setText("Секундомер");
- if (getCurrentState() == 0)
- run();
- break;
- case 1:
- mActionBarAddTimer
- .setImageResource(R.drawable.ic_actionbar_menu_add);
- mActionBarAddTimer.setClickable(true);
- mActionBarTitle.setText("Таймер");
- mStopwatchRunning = false;
- break;
- }
- }
- @Override
- public void onPageScrollStateChanged(int state) {
- }
- @Override
- public void onClick(View view) {
- if (view == mActionBarAddTimer) {
- startActivity(new Intent(mContext, TestAddNewTimer.class));
- } else if (view == mActionBarSwitchBtn) {
- mViewPager.setCurrentItem((mViewPager.getCurrentItem() - 1) * -1);
- } else if (view == mActionBarSettings) {
- if (HoneyCombSupported)
- startActivity(new Intent(mContext, Settings.class));
- else {
- // TODO: Add gingerbread settings
- }
- } else
- switch (getCurrentState()) {
- case 0:
- if (view == mStopwatchStartButton) {
- // Pause stopwatch
- mStopwatchRunning = false;
- mStopwatchPauseTime = System.currentTimeMillis()
- - mStopwatchStartTime;
- mStopwatchStartTime = 0;
- mStopwatchTimeTextView
- .setTextByTime(mStopwatchTimeTextMode ? mStopwatchPauseTime
- : mStopwatchPauseTime + mStopwatchFullTime);
- setButtonsTextByPaused();
- } else if (view == mStopwatchResetButton) {
- final String primaryLine = new String(
- Utils.convertTimeToStringHhMmSsMs(mStopwatchLapTime));
- final String secondaryLine = new String(
- Utils.convertTimeToStringHhMmSsMs(mStopwatchFullTime += mStopwatchLapTime));
- mStopwatchStartTime = System.currentTimeMillis();
- mStopwatchPrimaryLine.add(primaryLine);
- mStopwatchSecondaryLine.add(secondaryLine);
- updateListView();
- final int length = mStopwatchPrimaryLine.size();
- final SharedPreferences.Editor editor = mSharedPreferences
- .edit();
- editor.putString(
- Key.KEY_SHARED_PREFERENCES_STOPWATCH_PRIMARY_LINE
- + (length - 1), primaryLine);
- editor.putString(
- Key.KEY_SHARED_PREFERENCES_STOPWATCH_SECONDARY_LINE
- + (length - 1), secondaryLine).commit();
- }
- break;
- default:
- if (view == mStopwatchStartButton) {
- // Start stopwatch
- mStopwatchStartTime = System.currentTimeMillis()
- - mStopwatchPauseTime;
- mStopwatchPauseTime = 0;
- setButtonsTextByRunning();
- run();
- } else if (view == mStopwatchResetButton) {
- // Reset stopwatch
- mStopwatchRunning = false;
- mStopwatchPauseTime = 0;
- mStopwatchStartTime = 0;
- mStopwatchFullTime = 0;
- mStopwatchPrimaryLine = new ArrayList<String>();
- mStopwatchSecondaryLine = new ArrayList<String>();
- updateListView();
- setTimeTextToZero();
- mSharedPreferences.edit().clear().apply();
- }
- break;
- }
- }
- @Override
- public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
- String key) {
- if (key.equals(Settings.Key.KEY_STOPWATCH_MORE_TIMEMODE))
- updateStopwatchTimeTextModeValue(sharedPreferences);
- else if (key.equals(Settings.Key.KEY_GENERAL_ANIMATION_ENABLED)) {
- updateGeneralAnimationEnabledValues(sharedPreferences);
- if (!mAnimationEnabled && mAnimationScrollingEnabled
- && HoneyCombSupported)
- resetScrollingAnimation();
- } else if (key
- .equals(Settings.Key.KEY_GENERAL_ANIMATION_SCROLLING_ENABLED)) {
- updateGeneralAnimationScrollingEnabledValues(sharedPreferences);
- if (!mAnimationScrollingEnabled && HoneyCombSupported)
- resetScrollingAnimation();
- } else if (key.equals(Settings.Key.KEY_GENERAL_PAGE_MARGIN_ENABLED)) {
- updateGeneralPageMarginEnabledValues(sharedPreferences);
- }
- }
- private void resetScrollingAnimation() {
- mStopwatchView.setRotationY(0);
- mTimerView.setRotationY(0);
- mStopwatchView.setAlpha(1);
- mTimerView.setAlpha(1);
- }
- private void updateStopwatchTimeTextModeValue(SharedPreferences prefs) {
- mStopwatchTimeTextMode = prefs.getBoolean(
- Settings.Key.KEY_STOPWATCH_MORE_TIMEMODE, getResources()
- .getBoolean(R.bool.settings_stopwatch_more_timemode));
- }
- private void updateGeneralAnimationEnabledValues(SharedPreferences prefs) {
- mAnimationEnabled = prefs.getBoolean(
- Settings.Key.KEY_GENERAL_ANIMATION_ENABLED, getResources()
- .getBoolean(R.bool.settings_general_animation_enabled));
- }
- private void updateGeneralAnimationScrollingEnabledValues(
- SharedPreferences prefs) {
- mAnimationScrollingEnabled = prefs.getBoolean(
- Settings.Key.KEY_GENERAL_ANIMATION_SCROLLING_ENABLED,
- getResources().getBoolean(
- R.bool.settings_general_animation_scrolling_enabled));
- }
- private void updateGeneralPageMarginEnabledValues(SharedPreferences prefs) {
- mViewPager
- .setPageMargin(prefs
- .getBoolean(
- Settings.Key.KEY_GENERAL_PAGE_MARGIN_ENABLED,
- getResources()
- .getBoolean(
- R.bool.settings_general_interface_page_margin_enabled)) ? 10
- : 0);
- }
- private void run() {
- if (!mStopwatchRunning) {
- mStopwatchRunning = true;
- mRunnerHandler.post(mRunnerRunnable);
- }
- }
- private final Handler mRunnerHandler = new Handler();
- private final Runnable mRunnerRunnable = new Runnable() {
- private final static int MAX_FPS = 16666666, MIN_FPS = 33333333;
- private long last = System.nanoTime();
- private int sleepTime = 0;
- @Override
- public void run() {
- if (mStopwatchRunning) {
- final long now = System.nanoTime();
- final long fps = now - last;
- if (fps < MAX_FPS)
- sleepTime++;
- else if (fps > MIN_FPS && sleepTime > 0)
- sleepTime--;
- mStopwatchLapTime = System.currentTimeMillis()
- - mStopwatchStartTime;
- mStopwatchTimeTextView
- .setTextByTime(mStopwatchTimeTextMode ? mStopwatchLapTime
- : mStopwatchLapTime + mStopwatchFullTime);
- last = now;
- mRunnerHandler.postDelayed(mRunnerRunnable, sleepTime);
- }
- }
- };
- private int getCurrentState() {
- return mStopwatchStartTime != 0 ? 0 : mStopwatchPauseTime != 0 ? 1 : 2;
- }
- public void startByUsingData() {
- switch (getCurrentState()) {
- case 0:
- setButtonsTextByRunning();
- run();
- break;
- case 1:
- mStopwatchTimeTextView.setTextByTime(mStopwatchPauseTime);
- setButtonsTextByPaused();
- break;
- case 2:
- setTimeTextToZero();
- break;
- }
- }
- private void setTimeTextToZero() {
- mStopwatchTimeTextView.setTextByTime(0);
- setButtonsTextByPaused();
- }
- private void setButtonsTextByPaused() {
- mStopwatchStartButton.setText(getString(R.string.stopwatch_start));
- mStopwatchResetButton.setText(getString(R.string.stopwatch_reset));
- }
- private void setButtonsTextByRunning() {
- mStopwatchStartButton.setText(getString(R.string.stopwatch_pause));
- mStopwatchResetButton.setText(getString(R.string.stopwatch_lap));
- }
- private void getDataFromSharedPreferences() {
- mStopwatchStartTime = mSharedPreferences.getLong(
- Key.KEY_SHARED_PREFERENCES_STOPWATCH_START_TIME, 0);
- mStopwatchPauseTime = mSharedPreferences.getLong(
- Key.KEY_SHARED_PREFERENCES_STOPWATCH_PAUSE_TIME, 0);
- mStopwatchFullTime = mSharedPreferences.getLong(
- Key.KEY_SHARED_PREFERENCES_STOPWATCH_FULL_TIME, 0);
- int length = mSharedPreferences.getInt(
- Key.KEY_SHARED_PREFERENCES_STOPWATCH_LAPS_LENGTH, 0);
- if (mStopwatchPrimaryLine == null) {
- mStopwatchPrimaryLine = new ArrayList<String>();
- mStopwatchSecondaryLine = new ArrayList<String>();
- }
- for (int i = mStopwatchPrimaryLine.size(); i < length; i++) {
- mStopwatchPrimaryLine.add(new String(mSharedPreferences.getString(
- Key.KEY_SHARED_PREFERENCES_STOPWATCH_PRIMARY_LINE + i,
- LAPS_LOADING_ERROR)));
- mStopwatchSecondaryLine.add(new String(mSharedPreferences
- .getString(
- Key.KEY_SHARED_PREFERENCES_STOPWATCH_SECONDARY_LINE
- + i, LAPS_LOADING_ERROR)));
- }
- updateListView();
- startByUsingData();
- }
- private void updateListView() {
- mStopwatchListView.setAdapter(mStopwatchPrimaryLine.size() == 0 ? null
- : new TestListView(mContext, mStopwatchPrimaryLine
- .toArray(new String[0]), mStopwatchSecondaryLine
- .toArray(new String[0])));
- }
- }
Add Comment
Please, Sign In to add comment