package com.revo.display.views.fragment;

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;

import com.revo.display.R;
import com.revo.display.RevoApplication;
import com.revo.display.network.RFirebase;
import com.revo.display.views.custom.RSpeedometer;
import com.revo.display.views.custom.RBatteryMeter;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by sihrc on 9/20/14.
 */
public class DriverFragment extends RevoFragment {
    RFirebase RFirebase = RevoApplication.app.getFireBaseHelper();

    //Parameters for fake throttle
    int currentSpeed;
    int currentCharge;
    boolean accelerating;

    //Views for Display
    RSpeedometer RSpeedometer;
    RBatteryMeter RBatteryMeter;

    //For updating the speedometer
    Timer timer;
    boolean timerRunning;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.driver_fragment, container, false);
        RSpeedometer = (RSpeedometer) rootView.findViewById(R.id.speedometer);
        RBatteryMeter = (RBatteryMeter) rootView.findViewById((R.id.batterymeter));

        rootView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    accelerating = true;
                } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    accelerating = false;
                }
                return false;
            }
        });

        return rootView;
    }

    private void startRunning() {
        if (!timerRunning) {
            timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    new AsyncTask<Void, Integer, Void>() {
                        @Override
                        protected Void doInBackground(Void... voids) {
                            if (!accelerating) {
                                currentSpeed = currentSpeed > 0 ? currentSpeed - 2 : 0;
                                currentCharge = currentCharge > 0 ? currentCharge - 2 : 0;
                                Log.i("fake_throttle", "accelerating " + currentSpeed);
                            } else {
                                currentSpeed = currentSpeed < 100 ? (currentSpeed + (100 - currentSpeed)/10) : 100;
                                currentCharge = currentCharge < 100 ? (currentCharge + (100 - currentCharge)/10) : 100;
                                Log.i("fake_throttle", "accelerating " + currentSpeed);
                            }
                            return null;
                        }

                        @Override
                        protected void onPostExecute(Void aVoid) {
                            RSpeedometer.onSpeedChanged(currentSpeed);
                            RBatteryMeter.onChargeChanged(currentCharge);
                        }
                    }.execute();
                }
            }, 0, 250);
            timerRunning = true;
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (timerRunning && timer != null) {
            timerRunning = false;
            timer.cancel();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        startRunning();
    }

    @Override
    public String tag() {
        return DriverFragment.class.getSimpleName();
    }
}

@@ -57,68 +57,14 @@
 .os.
-AsyncTask;%0Aimport android.os.Bundle;%0Aimport android.util.Log
+Bundle
 ;%0Aim
@@ -101,41 +101,8 @@
 er;%0A
-import android.view.MotionEvent;%0A
 impo
@@ -157,79 +157,8 @@
 oup;
-%0Aimport android.view.ViewTreeObserver;%0Aimport android.widget.ImageView;
 %0A%0Aim
@@ -270,59 +270,8 @@
 se;%0A
-import com.revo.display.views.custom.RSpeedometer;%0A
 impo
@@ -322,33 +322,60 @@
 er;%0A
-%0A
 import 
-java.util.Tim
+com.revo.display.views.custom.RSpeedomet
 er;%0A
+%0A
 impo
@@ -392,20 +392,16 @@
 il.Timer
-Task
 ;%0A%0A/**%0A 
@@ -558,115 +558,8 @@
 );%0A%0A
-    //Parameters for fake throttle%0A    int currentSpeed;%0A    int currentCharge;%0A    boolean accelerating;%0A%0A
     
@@ -1089,2033 +1089,23 @@
    r
-ootView.setOnTouchListener(new View.OnTouchListener() %7B%0A            @Override%0A            public boolean onTouch(View view, MotionEvent motionEvent) %7B%0A                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) %7B%0A                    accelerating = true;%0A                %7D else if (motionEvent.getAction() == MotionEvent.ACTION_UP) %7B%0A                    accelerating = false;%0A                %7D%0A                return false;%0A            %7D%0A        %7D);%0A%0A        return rootView;%0A    %7D%0A%0A    private void startRunning() %7B%0A        if (!timerRunning) %7B%0A            timer = new Timer();%0A            timer.schedule(new TimerTask() %7B%0A                @Override%0A                public void run() %7B%0A                    new AsyncTask%3CVoid, Integer, Void%3E() %7B%0A                        @Override%0A                        protected Void doInBackground(Void... voids) %7B%0A                            if (!accelerating) %7B%0A                                currentSpeed = currentSpeed %3E 0 ? currentSpeed - 2 : 0;%0A                                currentCharge = currentCharge %3E 0 ? currentCharge - 2 : 0;%0A                                Log.i(%22fake_throttle%22, %22accelerating %22 + currentSpeed);%0A                            %7D else %7B%0A                                currentSpeed = currentSpeed %3C 100 ? (currentSpeed + (100 - currentSpeed)/10) : 100;%0A                                currentCharge = currentCharge %3C 100 ? (currentCharge + (100 - currentCharge)/10) : 100;%0A                                Log.i(%22fake_throttle%22, %22accelerating %22 + currentSpeed);%0A                            %7D%0A                            return null;%0A                        %7D%0A%0A                        @Override%0A                        protected void onPostExecute(Void aVoid) %7B%0A                            RSpeedometer.onSpeedChanged(currentSpeed);%0A                            RBatteryMeter.onChargeChanged(currentCharge);%0A                        %7D%0A                    %7D.execute();%0A                %7D%0A            %7D, 0, 250);%0A            timerRunning = true;%0A        %7D
+eturn rootView;
 %0A   
@@ -1303,108 +1303,8 @@
  %7D%0A%0A
-    @Override%0A    public void onResume() %7B%0A        super.onResume();%0A        startRunning();%0A    %7D%0A%0A
     

Remove testing throttle