();
24 |
25 | private static final int COUNT = 25;
26 |
27 | static {
28 | // Add some sample items.
29 | //getData(null);
30 | }
31 |
32 | public static void getWorkoutData(String key, final IGetAsyncListener complete) {
33 | if (WORKOUT_DATA_MAP.containsKey(key)) {
34 | if (complete != null)
35 | complete.onDataGet(true, WORKOUT_DATA_MAP.get(key));
36 | return;
37 | }
38 | FirebaseDatabase database = FirebaseDatabase.getInstance();
39 | DatabaseReference myRef = database.getReference("workoutCreator").child(key);
40 | myRef.addListenerForSingleValueEvent(new ValueEventListener() {
41 | @Override
42 | public void onDataChange(DataSnapshot dataSnapshot) {
43 | WorkoutData item = dataSnapshot.getValue(WorkoutData.class);
44 | WORKOUT_DATA_MAP.put(item.key, item);
45 |
46 | if (complete != null) {
47 | complete.onDataGet(true, item);
48 | }
49 | }
50 |
51 | @Override
52 | public void onCancelled(DatabaseError databaseError) {
53 |
54 | }
55 | });
56 | }
57 |
58 | public static void getWorkouts(final IGetAsyncListener complete) {
59 | if (WORKOUTS.size() > 0) {
60 | if (complete != null)
61 | complete.onDataGet(true, WORKOUTS);
62 | return;
63 | }
64 | FirebaseDatabase database = FirebaseDatabase.getInstance();
65 | DatabaseReference myRef = database.getReference("workouts");
66 | myRef.orderByChild("totalTicks").addListenerForSingleValueEvent(new ValueEventListener() {
67 | @Override
68 | public void onDataChange(DataSnapshot dataSnapshot) {
69 | for (DataSnapshot messageSnapshot: dataSnapshot.getChildren()) {
70 | Workout workout = messageSnapshot.getValue(Workout.class);
71 | addItem(workout);
72 | }
73 | if (complete != null) {
74 | complete.onDataGet(true, WORKOUTS);
75 | }
76 | }
77 |
78 | @Override
79 | public void onCancelled(DatabaseError databaseError) {
80 |
81 | }
82 | });
83 | }
84 |
85 | private static void addItem(Workout item) {
86 | WORKOUTS.add(item);
87 | WORKOUT_MAP.put(item.key, item);
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/app/src/main/java/com/trainerdb/trainerandroid/activities/AppCompatPreferenceActivity.java:
--------------------------------------------------------------------------------
1 | package com.trainerdb.trainerandroid.activities;
2 |
3 | import android.content.res.Configuration;
4 | import android.os.Bundle;
5 | import android.preference.PreferenceActivity;
6 | import android.support.annotation.LayoutRes;
7 | import android.support.annotation.Nullable;
8 | import android.support.v7.app.ActionBar;
9 | import android.support.v7.app.AppCompatDelegate;
10 | import android.support.v7.widget.Toolbar;
11 | import android.view.MenuInflater;
12 | import android.view.View;
13 | import android.view.ViewGroup;
14 |
15 | /**
16 | * A {@link android.preference.PreferenceActivity} which implements and proxies the necessary calls
17 | * to be used with AppCompat.
18 | */
19 | public abstract class AppCompatPreferenceActivity extends PreferenceActivity {
20 |
21 | private AppCompatDelegate mDelegate;
22 |
23 | @Override
24 | protected void onCreate(Bundle savedInstanceState) {
25 | getDelegate().installViewFactory();
26 | getDelegate().onCreate(savedInstanceState);
27 | super.onCreate(savedInstanceState);
28 | }
29 |
30 | @Override
31 | protected void onPostCreate(Bundle savedInstanceState) {
32 | super.onPostCreate(savedInstanceState);
33 | getDelegate().onPostCreate(savedInstanceState);
34 | }
35 |
36 | public ActionBar getSupportActionBar() {
37 | return getDelegate().getSupportActionBar();
38 | }
39 |
40 | public void setSupportActionBar(@Nullable Toolbar toolbar) {
41 | getDelegate().setSupportActionBar(toolbar);
42 | }
43 |
44 | @Override
45 | public MenuInflater getMenuInflater() {
46 | return getDelegate().getMenuInflater();
47 | }
48 |
49 | @Override
50 | public void setContentView(@LayoutRes int layoutResID) {
51 | getDelegate().setContentView(layoutResID);
52 | }
53 |
54 | @Override
55 | public void setContentView(View view) {
56 | getDelegate().setContentView(view);
57 | }
58 |
59 | @Override
60 | public void setContentView(View view, ViewGroup.LayoutParams params) {
61 | getDelegate().setContentView(view, params);
62 | }
63 |
64 | @Override
65 | public void addContentView(View view, ViewGroup.LayoutParams params) {
66 | getDelegate().addContentView(view, params);
67 | }
68 |
69 | @Override
70 | protected void onPostResume() {
71 | super.onPostResume();
72 | getDelegate().onPostResume();
73 | }
74 |
75 | @Override
76 | protected void onTitleChanged(CharSequence title, int color) {
77 | super.onTitleChanged(title, color);
78 | getDelegate().setTitle(title);
79 | }
80 |
81 | @Override
82 | public void onConfigurationChanged(Configuration newConfig) {
83 | super.onConfigurationChanged(newConfig);
84 | getDelegate().onConfigurationChanged(newConfig);
85 | }
86 |
87 | @Override
88 | protected void onStop() {
89 | super.onStop();
90 | getDelegate().onStop();
91 | }
92 |
93 | @Override
94 | protected void onDestroy() {
95 | super.onDestroy();
96 | getDelegate().onDestroy();
97 | }
98 |
99 | public void invalidateOptionsMenu() {
100 | getDelegate().invalidateOptionsMenu();
101 | }
102 |
103 | private AppCompatDelegate getDelegate() {
104 | if (mDelegate == null) {
105 | mDelegate = AppCompatDelegate.create(this, null);
106 | }
107 | return mDelegate;
108 | }
109 | }
110 |
--------------------------------------------------------------------------------
/app/src/main/java/com/trainerdb/trainerandroid/train/TelemetryView.java:
--------------------------------------------------------------------------------
1 | package com.trainerdb.trainerandroid.train;
2 |
3 | import android.content.Context;
4 | import android.support.v4.content.ContextCompat;
5 | import android.util.AttributeSet;
6 | import android.view.LayoutInflater;
7 | import android.view.View;
8 | import android.view.ViewGroup;
9 | import android.widget.LinearLayout;
10 | import android.widget.TextView;
11 |
12 | import com.trainerdb.trainerandroid.AutoResizeTextView;
13 | import com.trainerdb.trainerandroid.R;
14 |
15 | /**
16 | * Created by Daniel on 07/07/2016.
17 | */
18 | public class TelemetryView extends LinearLayout {
19 | private TextView header;
20 | private AutoResizeTextView data;
21 | private TelemetryEnum type;
22 |
23 | public TelemetryView(Context context, AttributeSet attrs) {
24 | super(context, attrs);
25 |
26 | LayoutInflater inflater = (LayoutInflater) context
27 | .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
28 |
29 | inflater.inflate(R.layout.view_telemetry, this, true);
30 |
31 | header = (TextView) getChildAt(0);
32 |
33 | data = (AutoResizeTextView) getChildAt(1);
34 | }
35 |
36 | public TelemetryView(Context context) {
37 | this(context, null);
38 | }
39 |
40 | public void setType(TelemetryEnum type) {
41 | this.type = type;
42 | this.header.setText(type.toString());
43 |
44 | switch (type) {
45 | case CADENCE:
46 | data.setTextColor(ContextCompat.getColor(this.getContext(), R.color.trainCadence));
47 | break;
48 | case LP_CAD:
49 | data.setTextColor(ContextCompat.getColor(this.getContext(), R.color.trainCadence));
50 | break;
51 | case KM:
52 | data.setTextColor(ContextCompat.getColor(this.getContext(), R.color.trainDistance));
53 | break;
54 | case HR:
55 | data.setTextColor(ContextCompat.getColor(this.getContext(), R.color.trainHearRate));
56 | break;
57 | case IF:
58 | data.setTextColor(ContextCompat.getColor(this.getContext(), R.color.trainPower));
59 | break;
60 | case LAP:
61 | data.setTextColor(ContextCompat.getColor(this.getContext(), R.color.trainLap));
62 | break;
63 | case LP_HR:
64 | data.setTextColor(ContextCompat.getColor(this.getContext(), R.color.trainLapHr));
65 | break;
66 | case LP_POWER:
67 | data.setTextColor(ContextCompat.getColor(this.getContext(), R.color.trainLapPower));
68 | break;
69 | case NP:
70 | data.setTextColor(ContextCompat.getColor(this.getContext(), R.color.trainPower));
71 | break;
72 | case POWER:
73 | data.setTextColor(ContextCompat.getColor(this.getContext(), R.color.trainPower));
74 | break;
75 | case POWER_30S:
76 | data.setTextColor(ContextCompat.getColor(this.getContext(), R.color.trainPower));
77 | break;
78 | case SPEED:
79 | data.setTextColor(ContextCompat.getColor(this.getContext(), R.color.trainSpeed));
80 | break;
81 | case TARGET:
82 | data.setTextColor(ContextCompat.getColor(this.getContext(), R.color.trainTarget));
83 | break;
84 | case TSS:
85 | data.setTextColor(ContextCompat.getColor(this.getContext(), R.color.trainPower));
86 | break;
87 | }
88 | }
89 |
90 | public void setValue(String text) {
91 | this.data.setText(text);
92 | }
93 | }
94 |
--------------------------------------------------------------------------------
/app/src/main/java/com/trainerdb/trainerandroid/activities/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.trainerdb.trainerandroid.activities;
2 |
3 | import android.content.Intent;
4 | import android.os.Bundle;
5 | import android.support.design.widget.FloatingActionButton;
6 | import android.support.design.widget.Snackbar;
7 | import android.view.View;
8 | import android.support.design.widget.NavigationView;
9 | import android.support.v4.view.GravityCompat;
10 | import android.support.v4.widget.DrawerLayout;
11 | import android.support.v7.app.ActionBarDrawerToggle;
12 | import android.support.v7.app.AppCompatActivity;
13 | import android.support.v7.widget.Toolbar;
14 | import android.view.Menu;
15 | import android.view.MenuItem;
16 |
17 | import com.trainerdb.trainerandroid.R;
18 |
19 | public class MainActivity extends AppCompatActivity
20 | implements NavigationView.OnNavigationItemSelectedListener {
21 |
22 | @Override
23 | protected void onCreate(Bundle savedInstanceState) {
24 | super.onCreate(savedInstanceState);
25 | setContentView(R.layout.activity_main);
26 | Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
27 | setSupportActionBar(toolbar);
28 |
29 | DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
30 | ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
31 | this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
32 | drawer.setDrawerListener(toggle);
33 | toggle.syncState();
34 |
35 | NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
36 | navigationView.setNavigationItemSelectedListener(this);
37 | }
38 |
39 | @Override
40 | public void onBackPressed() {
41 | DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
42 | if (drawer.isDrawerOpen(GravityCompat.START)) {
43 | drawer.closeDrawer(GravityCompat.START);
44 | } else {
45 | super.onBackPressed();
46 | }
47 | }
48 |
49 | @Override
50 | public boolean onCreateOptionsMenu(Menu menu) {
51 | // Inflate the menu; this adds items to the action bar if it is present.
52 | getMenuInflater().inflate(R.menu.main, menu);
53 | return true;
54 | }
55 |
56 | @Override
57 | public boolean onOptionsItemSelected(MenuItem item) {
58 | // Handle action bar item clicks here. The action bar will
59 | // automatically handle clicks on the Home/Up button, so long
60 | // as you specify a parent activity in AndroidManifest.xml.
61 | int id = item.getItemId();
62 |
63 | //noinspection SimplifiableIfStatement
64 | if (id == R.id.action_settings) {
65 | Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
66 | startActivity(intent);
67 | return true;
68 | }
69 |
70 | return super.onOptionsItemSelected(item);
71 | }
72 |
73 | @SuppressWarnings("StatementWithEmptyBody")
74 | @Override
75 | public boolean onNavigationItemSelected(MenuItem item) {
76 | // Handle navigation view item clicks here.
77 | int id = item.getItemId();
78 |
79 | if (id == R.id.nav_workouts) {
80 | Intent intent = new Intent(this, WorkoutListActivity.class);
81 | this.startActivity(intent);
82 | } else if (id == R.id.nav_train_files) {
83 | Intent intent = new Intent(this, TrainFileListActivity.class);
84 | this.startActivity(intent);
85 | } else if (id == R.id.nav_send) {
86 |
87 | }
88 |
89 | DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
90 | drawer.closeDrawer(GravityCompat.START);
91 | return true;
92 | }
93 | }
94 |
--------------------------------------------------------------------------------
/app/src/main/java/com/trainerdb/trainerandroid/activities/WorkoutDetailFragment.java:
--------------------------------------------------------------------------------
1 | package com.trainerdb.trainerandroid.activities;
2 |
3 | import android.app.Activity;
4 | import android.support.design.widget.CollapsingToolbarLayout;
5 | import android.os.Bundle;
6 | import android.support.v4.app.Fragment;
7 | import android.view.LayoutInflater;
8 | import android.view.View;
9 | import android.view.ViewGroup;
10 | import android.widget.TextView;
11 |
12 | import com.trainerdb.trainerandroid.R;
13 | import com.trainerdb.trainerandroid.TrainApplication;
14 | import com.trainerdb.trainerandroid.WorkoutView;
15 | import com.trainerdb.trainerandroid.data.Workout;
16 | import com.trainerdb.trainerandroid.data.WorkoutData;
17 | import com.trainerdb.trainerandroid.data.WorkoutService;
18 |
19 | /**
20 | * A fragment representing a single Workout detail screen.
21 | * This fragment is either contained in a {@link WorkoutListActivity}
22 | * in two-pane mode (on tablets) or a {@link WorkoutDetailActivity}
23 | * on handsets.
24 | */
25 | public class WorkoutDetailFragment extends Fragment {
26 | /**
27 | * The fragment argument representing the item ID that this fragment
28 | * represents.
29 | */
30 | public static final String ARG_ITEM_ID = "item_id";
31 |
32 | /**
33 | * The dummy content this fragment is presenting.
34 | */
35 | private Workout mItem;
36 | private WorkoutData mData;
37 |
38 | /**
39 | * Mandatory empty constructor for the fragment manager to instantiate the
40 | * fragment (e.g. upon screen orientation changes).
41 | */
42 | public WorkoutDetailFragment() {
43 | }
44 |
45 | @Override
46 | public void onCreate(Bundle savedInstanceState) {
47 | super.onCreate(savedInstanceState);
48 |
49 | if (getArguments().containsKey(ARG_ITEM_ID)) {
50 | // Load the dummy content specified by the fragment
51 | // arguments. In a real-world scenario, use a Loader
52 | // to load content from a content provider.
53 | mItem = WorkoutService.WORKOUT_MAP.get(getArguments().getString(ARG_ITEM_ID));
54 | mData = WorkoutService.WORKOUT_DATA_MAP.get(getArguments().getString(ARG_ITEM_ID));
55 |
56 | Activity activity = this.getActivity();
57 | CollapsingToolbarLayout appBarLayout = (CollapsingToolbarLayout) activity.findViewById(R.id.toolbar_layout);
58 | WorkoutView wokroutView = (WorkoutView) activity.findViewById(R.id.workout_detail);
59 |
60 | if (appBarLayout != null) {
61 | appBarLayout.setTitle(mItem.name);
62 | wokroutView.setWorkout(mItem);
63 | }
64 | }
65 | }
66 |
67 | @Override
68 | public View onCreateView(LayoutInflater inflater, ViewGroup container,
69 | Bundle savedInstanceState) {
70 | View rootView = inflater.inflate(R.layout.workout_detail, container, false);
71 |
72 | // Show the dummy content as text in a TextView.
73 | if (mItem != null) {
74 | //((TextView) rootView.findViewById(R.id.workout_detail)).setText(mItem.name);
75 | ((TextView) rootView.findViewById(R.id.tvWorkoutDetailName)).setText(mItem.name);
76 | ((TextView) rootView.findViewById(R.id.tvWorkoutDetailDescription)).setText(mData.description);
77 | ((TextView) rootView.findViewById(R.id.tvWorkoutDetailGoal)).setText(mData.goals);
78 | ((TextView) rootView.findViewById(R.id.tvWorkoutDetailDuration)).setText(TrainApplication.formatTime(mData.totalTicks));
79 | ((TextView) rootView.findViewById(R.id.tvWorkoutDetailIf)).setText(String.valueOf(mData.intensityFactor));
80 | ((TextView) rootView.findViewById(R.id.tvWorkoutDetailTss)).setText(String.valueOf(mData.tss));
81 | }
82 |
83 | return rootView;
84 | }
85 | }
86 |
--------------------------------------------------------------------------------
/app/src/main/java/com/trainerdb/trainerandroid/activities/WorkoutDetailActivity.java:
--------------------------------------------------------------------------------
1 | package com.trainerdb.trainerandroid.activities;
2 |
3 | import android.content.Intent;
4 | import android.os.Bundle;
5 | import android.support.design.widget.FloatingActionButton;
6 | import android.support.design.widget.Snackbar;
7 | import android.support.v7.widget.Toolbar;
8 | import android.view.View;
9 | import android.support.v7.app.AppCompatActivity;
10 | import android.support.v7.app.ActionBar;
11 | import android.view.MenuItem;
12 |
13 | import com.trainerdb.trainerandroid.R;
14 |
15 | /**
16 | * An activity representing a single Workout detail screen. This
17 | * activity is only used narrow width devices. On tablet-size devices,
18 | * item details are presented side-by-side with a list of items
19 | * in a {@link WorkoutListActivity}.
20 | */
21 | public class WorkoutDetailActivity extends AppCompatActivity {
22 |
23 | @Override
24 | protected void onCreate(Bundle savedInstanceState) {
25 | super.onCreate(savedInstanceState);
26 | setContentView(R.layout.activity_workout_detail);
27 | Toolbar toolbar = (Toolbar) findViewById(R.id.detail_toolbar);
28 | setSupportActionBar(toolbar);
29 |
30 | FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
31 | fab.setOnClickListener(new View.OnClickListener() {
32 | @Override
33 | public void onClick(View view) {
34 | Intent intent = new Intent(view.getContext(), TrainActivity.class);
35 | intent.putExtra("workout_key", getIntent().getStringExtra(WorkoutDetailFragment.ARG_ITEM_ID));
36 |
37 | view.getContext().startActivity(intent);
38 | }
39 | });
40 |
41 | // Show the Up button in the action bar.
42 | ActionBar actionBar = getSupportActionBar();
43 | if (actionBar != null) {
44 | actionBar.setDisplayHomeAsUpEnabled(true);
45 | }
46 |
47 | // savedInstanceState is non-null when there is fragment state
48 | // saved from previous configurations of this activity
49 | // (e.g. when rotating the screen from portrait to landscape).
50 | // In this case, the fragment will automatically be re-added
51 | // to its container so we don't need to manually add it.
52 | // For more information, see the Fragments API guide at:
53 | //
54 | // http://developer.android.com/guide/components/fragments.html
55 | //
56 | if (savedInstanceState == null) {
57 | // Create the detail fragment and add it to the activity
58 | // using a fragment transaction.
59 | Bundle arguments = new Bundle();
60 | arguments.putString(WorkoutDetailFragment.ARG_ITEM_ID,
61 | getIntent().getStringExtra(WorkoutDetailFragment.ARG_ITEM_ID));
62 | WorkoutDetailFragment fragment = new WorkoutDetailFragment();
63 | fragment.setArguments(arguments);
64 | getSupportFragmentManager().beginTransaction()
65 | .add(R.id.workout_detail_container, fragment)
66 | .commit();
67 | }
68 | }
69 |
70 | @Override
71 | public boolean onOptionsItemSelected(MenuItem item) {
72 | int id = item.getItemId();
73 | if (id == android.R.id.home) {
74 | // This ID represents the Home or Up button. In the case of this
75 | // activity, the Up button is shown. For
76 | // more details, see the Navigation pattern on Android Design:
77 | //
78 | // http://developer.android.com/design/patterns/navigation.html#up-vs-back
79 | //
80 | //navigateUpTo(new Intent(this, WorkoutListActivity.class));
81 | super.onBackPressed();
82 | return true;
83 | }
84 | return super.onOptionsItemSelected(item);
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/app/src/main/java/com/trainerdb/trainerandroid/train/StopWatch.java:
--------------------------------------------------------------------------------
1 | package com.trainerdb.trainerandroid.train;
2 |
3 | import com.trainerdb.trainerandroid.TrainApplication;
4 |
5 | /**
6 | * Created by Daniel on 23/06/2016.
7 | */
8 | public class StopWatch {
9 |
10 | /**
11 | * The start time.
12 | */
13 | private long startTime = -1;
14 | /**
15 | * The stop time.
16 | */
17 | private long stopTime = -1;
18 |
19 | /**
20 | * Constructor.
21 | */
22 | public StopWatch() {
23 | }
24 |
25 | /**
26 | * Start the stopwatch.
27 | *
28 | * This method starts a new timing session, clearing any previous values.
29 | */
30 | public void start() {
31 | stopTime = -1;
32 | startTime = System.currentTimeMillis();
33 | }
34 |
35 |
36 | public long restart() {
37 | long elapsed = elapsed();
38 | start();
39 | return elapsed;
40 | }
41 |
42 | /**
43 | * Stop the stopwatch.
44 | *
45 | * This method ends a new timing session, allowing the time to be retrieved.
46 | */
47 | public void stop() {
48 | stopTime = System.currentTimeMillis();
49 | }
50 |
51 | /**
52 | * Reset the stopwatch.
53 | *
54 | * This method clears the internal values to allow the object to be reused.
55 | */
56 | public void reset() {
57 | startTime = -1;
58 | stopTime = -1;
59 | }
60 |
61 | /**
62 | * Split the time.
63 | *
64 | * This method sets the stop time of the watch to allow a time to be extracted.
65 | * The start time is unaffected, enabling {@link #unsplit()} to contine the
66 | * timing from the original start point.
67 | */
68 | public void split() {
69 | stopTime = System.currentTimeMillis();
70 | }
71 |
72 | /**
73 | * Remove a split.
74 | *
75 | * This method clears the stop time. The start time is unaffected, enabling
76 | * timing from the original start point to continue.
77 | */
78 | public void unsplit() {
79 | stopTime = -1;
80 | }
81 |
82 | /**
83 | * Suspend the stopwatch for later resumption.
84 | *
85 | * This method suspends the watch until it is resumed. The watch will not include
86 | * time between the suspend and resume calls in the total time.
87 | */
88 | public void suspend() {
89 | stopTime = System.currentTimeMillis();
90 | }
91 |
92 | /**
93 | * Resume the stopwatch after a suspend.
94 | *
95 | * This method resumes the watch after it was suspended. The watch will not include
96 | * time between the suspend and resume calls in the total time.
97 | */
98 | public void resume() {
99 | startTime += (System.currentTimeMillis() - stopTime);
100 | stopTime = -1;
101 | }
102 |
103 | /**
104 | * Get the time on the stopwatch.
105 | *
106 | * This is either the time between start and latest split, between start
107 | * and stop, or the time between the start and the moment this method is called.
108 | *
109 | * @return the time in milliseconds
110 | */
111 | public long elapsed() {
112 | if (stopTime == -1) {
113 | if (startTime == -1) {
114 | return 0;
115 | }
116 | return (System.currentTimeMillis() - this.startTime);
117 | }
118 | return (this.stopTime - this.startTime);
119 | }
120 |
121 | /**
122 | * Gets a summary of the time that the stopwatch recorded as a string.
123 | *
124 | * The format used is ISO8601-like,
125 | * hours:minutes:seconds.milliseconds.
126 | *
127 | * @return the time as a String
128 | */
129 | public String toString() {
130 | return TrainApplication.formatTime(elapsed());
131 | }
132 |
133 |
134 |
135 | }
136 |
--------------------------------------------------------------------------------
/app/src/main/java/com/trainerdb/trainerandroid/activities/WorkoutListOptions.java:
--------------------------------------------------------------------------------
1 | package com.trainerdb.trainerandroid.activities;
2 |
3 | import android.os.Parcel;
4 | import android.os.Parcelable;
5 |
6 | import java.io.Serializable;
7 | import java.util.ArrayList;
8 | import java.util.List;
9 |
10 | /**
11 | * Created by dcotrim on 22/09/2016.
12 | */
13 | public class WorkoutListOptions implements Serializable, Parcelable {
14 | public enum ItemType {
15 | sortName, sortDuration, sortIntensity, sortTss,
16 | filterZoneEndurance, filterZoneTempo, filterZoneSweetSpot, filterZoneThreshold, filterZoneVo2, filterZoneAnaerobic, filterZoneSprint, filterZoneStrength,
17 | filterDurationLess1h, filterDuration60_90, filterDuration90_2h, filterDuration2_3h, filterDuration3hmore,
18 | filterFavorite
19 | }
20 |
21 | private List list;
22 |
23 | public WorkoutListOptions() {
24 |
25 | }
26 |
27 | public void prepare() {
28 | list = new ArrayList<>();
29 |
30 | WorkoutListParent sort = new WorkoutListParent("Sort by", true);
31 | sort.filters.add(sort.newSort(ItemType.sortName, "Name", "A - Z", "Z - A"));
32 | sort.filters.add(sort.newSort(ItemType.sortDuration, "Duration", "Shortest", "Longest"));
33 | sort.filters.add(sort.newSort(ItemType.sortIntensity, "Intensity", "Lowest", "Highest"));
34 | sort.filters.add(sort.newSort(ItemType.sortTss, "TSS", "Lowest", "Highest"));
35 | list.add(sort);
36 |
37 | WorkoutListParent filterZone = new WorkoutListParent("Filter by tags", false);
38 | filterZone.filters.add(filterZone.newFilter(ItemType.filterZoneEndurance, "Endurance", true));
39 | filterZone.filters.add(filterZone.newFilter(ItemType.filterZoneTempo, "Tempo", true));
40 | filterZone.filters.add(filterZone.newFilter(ItemType.filterZoneSweetSpot, "Sweet Spot", true));
41 | filterZone.filters.add(filterZone.newFilter(ItemType.filterZoneThreshold, "Threshold", true));
42 | filterZone.filters.add(filterZone.newFilter(ItemType.filterZoneVo2, "VO2 Max", true));
43 | filterZone.filters.add(filterZone.newFilter(ItemType.filterZoneAnaerobic, "Anaerobic", true));
44 | filterZone.filters.add(filterZone.newFilter(ItemType.filterZoneSprint, "Sprint", true));
45 | filterZone.filters.add(filterZone.newFilter(ItemType.filterZoneStrength, "Strength", true));
46 |
47 | list.add(filterZone);
48 |
49 | WorkoutListParent filterDuration = new WorkoutListParent("Filter by duration", false);
50 | filterDuration.filters.add(filterDuration.newFilter(ItemType.filterDurationLess1h, "Less than 1h", true));
51 | filterDuration.filters.add(filterDuration.newFilter(ItemType.filterDuration60_90, "60 - 90min", true));
52 | filterDuration.filters.add(filterDuration.newFilter(ItemType.filterDuration90_2h, "90min - 2h", true));
53 | filterDuration.filters.add(filterDuration.newFilter(ItemType.filterDuration2_3h, "2 - 3h", true));
54 | filterDuration.filters.add(filterDuration.newFilter(ItemType.filterDuration3hmore, "More than 3h", true));
55 | list.add(filterDuration);
56 |
57 | WorkoutListParent filterOptions = new WorkoutListParent("Filter Options", false);
58 | filterOptions.filters.add(filterDuration.newFilter(ItemType.filterFavorite, "Show only Favorites", false));
59 | list.add(filterOptions);
60 | }
61 |
62 | public List getList() {
63 | return list;
64 | }
65 |
66 | protected WorkoutListOptions(Parcel in) {
67 | if (in.readByte() == 0x01) {
68 | list = new ArrayList();
69 | in.readList(list, WorkoutListParent.class.getClassLoader());
70 | } else {
71 | list = null;
72 | }
73 | }
74 |
75 | @Override
76 | public int describeContents() {
77 | return 0;
78 | }
79 |
80 | @Override
81 | public void writeToParcel(Parcel dest, int flags) {
82 | if (list == null) {
83 | dest.writeByte((byte) (0x00));
84 | } else {
85 | dest.writeByte((byte) (0x01));
86 | dest.writeList(list);
87 | }
88 | }
89 |
90 | @SuppressWarnings("unused")
91 | public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
92 | @Override
93 | public WorkoutListOptions createFromParcel(Parcel in) {
94 | return new WorkoutListOptions(in);
95 | }
96 |
97 | @Override
98 | public WorkoutListOptions[] newArray(int size) {
99 | return new WorkoutListOptions[size];
100 | }
101 | };
102 | }
103 |
--------------------------------------------------------------------------------
/app/src/main/java/com/trainerdb/trainerandroid/TrainApplication.java:
--------------------------------------------------------------------------------
1 | package com.trainerdb.trainerandroid;
2 |
3 | import android.app.Application;
4 | import android.content.Context;
5 | import android.content.SharedPreferences;
6 | import android.preference.PreferenceManager;
7 |
8 | import com.trainerdb.trainerandroid.data.PowerZone;
9 | import com.trainerdb.trainerandroid.train.DeviceConfiguration;
10 | import com.trainerdb.trainerandroid.train.TrainController;
11 |
12 | import java.util.ArrayList;
13 | import java.util.HashSet;
14 | import java.util.List;
15 | import java.util.Set;
16 |
17 | /**
18 | * Created by dcotrim on 21/09/2016.
19 | */
20 | public class TrainApplication extends Application {
21 | private static Context context;
22 | private static TrainController trainController;
23 | private static SharedPreferences preferences;
24 |
25 | public static String formatTime(long milliSeconds) {
26 | long second = (milliSeconds / 1000) % 60;
27 | long minute = (milliSeconds / (1000 * 60)) % 60;
28 | long hour = (milliSeconds / (1000 * 60 * 60)) % 24;
29 | if (hour > 0)
30 | return String.format("%d:%02d:%02d", hour, minute, second);
31 | else
32 | return String.format("%02d:%02d", minute, second);
33 |
34 | }
35 |
36 | public void onCreate() {
37 | super.onCreate();
38 | context = getApplicationContext();
39 | preferences = PreferenceManager.getDefaultSharedPreferences(context);
40 | }
41 |
42 | public static int getFTP() {
43 | return Integer.parseInt(preferences.getString("ftp", "200"));
44 | }
45 |
46 | public static Context getAppContext() {
47 | return TrainApplication.context;
48 | }
49 |
50 | public static int getVirtualPowerId() {
51 | return Integer.parseInt(preferences.getString("virtual", "0"));
52 | }
53 | public static DeviceConfiguration.DeviceType getDeviceType() {
54 | return DeviceConfiguration.DeviceType.valueOf(preferences.getString("device", "DEV_ANTLOCAL"));
55 | }
56 | public static void setTrainController(TrainController newTrainController) {
57 | if (trainController != null) {
58 | trainController.stop();
59 | trainController.disconnect();
60 | }
61 | trainController = newTrainController;
62 | }
63 |
64 | public static TrainController getTrainController() {
65 | return trainController;
66 | }
67 |
68 | public static boolean isFavorite(String key) {
69 | Set values = preferences.getStringSet("workout_favorite", null);
70 | if (values != null && values.contains(key))
71 | return true;
72 | return false;
73 | }
74 |
75 | public static void addFavorite(String key) {
76 | Set def = new HashSet<>();
77 | Set values = new HashSet<>(preferences.getStringSet("workout_favorite", def));
78 | if (!values.contains(key)) {
79 | values.add(key);
80 | preferences.edit().putStringSet("workout_favorite", values).apply();
81 | }
82 | }
83 |
84 | public static void removeFavorite(String key) {
85 | Set def = new HashSet<>();
86 | Set values = new HashSet<>(preferences.getStringSet("workout_favorite", def));
87 | if (values.contains(key)) {
88 | values.remove(key);
89 | preferences.edit().putStringSet("workout_favorite", values).apply();
90 | }
91 | }
92 |
93 | private static double calcPerimeter(double rim, double tire) {
94 | if (rim > 0 && tire > 0) {
95 | return Math.round((rim + tire) * Math.PI);
96 | }
97 | return 0;
98 | }
99 |
100 | public static double getWheelCircunference() {
101 | double rim = Double.parseDouble(preferences.getString("rim", "622"));
102 | double tire = Double.parseDouble(preferences.getString("tire", "46"));
103 | return calcPerimeter(rim, tire);
104 | }
105 |
106 | public static List getPowerZones() {
107 | int ftp = getFTP();
108 | List powerZones = new ArrayList<>();
109 | powerZones.add(0, new PowerZone("Active Recovery", 0, (int) (0.55 * ftp)));
110 | powerZones.add(1, new PowerZone("Endurance", (int) (0.55 * ftp), (int) (0.75 * ftp)));
111 | powerZones.add(2, new PowerZone("Tempo", (int) (0.75 * ftp), (int) (0.90 * ftp)));
112 | powerZones.add(3, new PowerZone("Lactate Threshold", (int) (0.90 * ftp), (int) (1.05 * ftp)));
113 | powerZones.add(4, new PowerZone("VO2 Max", (int) (1.05 * ftp), (int) (1.2 * ftp)));
114 | powerZones.add(5, new PowerZone("Anaerobic Capacity", (int) (1.2 * ftp), (int) (1.5 * ftp)));
115 | powerZones.add(6, new PowerZone("Neuromuscular Power", (int) (1.5 * ftp), Integer.MAX_VALUE));
116 | return powerZones;
117 | }
118 | }
119 |
--------------------------------------------------------------------------------
/app/src/main/java/com/trainerdb/trainerandroid/train/LapControl.java:
--------------------------------------------------------------------------------
1 | package com.trainerdb.trainerandroid.train;
2 |
3 | import com.trainerdb.trainerandroid.TrainApplication;
4 | import java.io.Serializable;
5 |
6 | /**
7 | * Created by Daniel on 30/06/2016.
8 | */
9 |
10 | public class LapControl implements Serializable {
11 | private int count;
12 | private double sumHr, sumWatts, sumCadence, sumSpeed;
13 | private double wattsMin, wattsMax, hrMin, hrMax, cadenceMin, cadenceMax, speedMin, speedMax;
14 | private double distanceStart, distanceEnd;
15 | private int lapNum;
16 | private String name;
17 | private long start, end;
18 | private boolean isEmptyDistance;
19 | private NormalizedPower npLap;
20 | private int ftp = TrainApplication.getFTP();
21 | private int recPerSec;
22 |
23 | public LapControl() {
24 | clear();
25 | }
26 |
27 | public LapControl(int recPerSec, int lapNum, String name, long start, long end) {
28 | super();
29 | this.lapNum = lapNum;
30 | this.name = name;
31 | this.start = start;
32 | this.end = end;
33 | this.recPerSec = recPerSec;
34 | npLap = new NormalizedPower(ftp, recPerSec);
35 | }
36 |
37 | public void clear() {
38 | count = lapNum = 0;
39 | start = end = 0;
40 | name = "";
41 | sumHr = sumWatts = sumCadence = sumSpeed = 0;
42 | wattsMax = hrMax = cadenceMax = speedMax = 0;
43 | distanceEnd = distanceStart = 0;
44 | cadenceMin = Double.MAX_VALUE;
45 | speedMin = Double.MAX_VALUE;
46 | wattsMin = Double.MAX_VALUE;
47 | hrMin = Double.MAX_VALUE;
48 | isEmptyDistance = true;
49 | }
50 |
51 | public void add() {
52 | count++;
53 | }
54 |
55 | public void setHr(double value) {
56 | sumHr += value;
57 | if (value > hrMax) hrMax = value;
58 | else if (value < hrMin) hrMin = value;
59 | }
60 |
61 | public void setWatts(double value) {
62 | sumWatts += value;
63 | if (value > wattsMax) wattsMax = value;
64 | else if (value < wattsMin) wattsMin = value;
65 |
66 | this.npLap.set(value);
67 | }
68 |
69 | public void setCadence(double value) {
70 | sumCadence += value;
71 | if (value > cadenceMax) cadenceMax = value;
72 | else if (value < cadenceMin) cadenceMin = value;
73 | }
74 |
75 | public void setSpeed(double value) {
76 | sumSpeed += value;
77 | if (value > speedMax) speedMax = value;
78 | else if (value < speedMin) speedMin = value;
79 | }
80 |
81 | public double getHr() {
82 | return sumHr / count;
83 | }
84 |
85 | public double getCadence() {
86 | return sumCadence / count;
87 | }
88 |
89 | public double getSpeed() {
90 | return sumSpeed / count;
91 | }
92 |
93 | public double getWatts() {
94 | return sumWatts / count;
95 | }
96 |
97 | public double getHrMin() {
98 | return hrMin;
99 | }
100 |
101 | public double getHrMax() {
102 | return hrMax;
103 | }
104 |
105 | public double getCadenceMax() {
106 | return cadenceMax;
107 | }
108 |
109 | public double getCadenceMin() {
110 | return cadenceMin;
111 | }
112 |
113 | public double getSpeedMax() {
114 | return speedMax;
115 | }
116 |
117 | public double getSpeedMin() {
118 | return speedMin;
119 | }
120 |
121 | public double getWattsMax() {
122 | return wattsMax;
123 | }
124 |
125 | public double getWattsMin() {
126 | return wattsMin;
127 | }
128 |
129 | public void setDistance(double distance) {
130 | if (isEmptyDistance) {
131 | distanceStart = distance;
132 | isEmptyDistance = false;
133 | }
134 | distanceEnd = distance;
135 | }
136 |
137 | public int getCount() {
138 | return count;
139 | }
140 |
141 | public double getDistance() {
142 | return distanceEnd - distanceStart;
143 | }
144 |
145 | public boolean isRunning(long now) {
146 | return start <= now && end >= now ? true : false;
147 | }
148 |
149 | public long getEnd() {
150 | return end;
151 | }
152 |
153 | public void setEnd(long end) {
154 | this.end = end;
155 | }
156 |
157 | public int getLapNum() {
158 | return lapNum;
159 | }
160 |
161 | public void setLapNum(int lapNum) {
162 | this.lapNum = lapNum;
163 | }
164 |
165 | public String getName() {
166 | return name;
167 | }
168 |
169 | public void setName(String name) {
170 | this.name = name;
171 | }
172 |
173 | public long getStart() {
174 | return start;
175 | }
176 |
177 | public void setStart(long start) {
178 | this.start = start;
179 | }
180 |
181 | public NormalizedPower getNp() {
182 | return npLap;
183 | }
184 |
185 | public double getKj() {
186 | return sumWatts / (recPerSec * 1000);
187 | }
188 | }
189 |
--------------------------------------------------------------------------------
/app/src/main/java/com/trainerdb/trainerandroid/train/TrainState.java:
--------------------------------------------------------------------------------
1 | package com.trainerdb.trainerandroid.train;
2 |
3 | import android.net.Uri;
4 | import android.support.v4.content.FileProvider;
5 | import com.trainerdb.trainerandroid.TrainApplication;
6 | import com.trainerdb.trainerandroid.data.LapPoint;
7 | import com.trainerdb.trainerandroid.data.WorkoutData;
8 |
9 | import java.io.File;
10 | import java.io.Serializable;
11 | import java.util.ArrayList;
12 | import java.util.List;
13 |
14 | /**
15 | * Created by Daniel on 11/07/2016.
16 | */
17 | public class TrainState implements Serializable {
18 | public WorkoutData workout;
19 | //public WorkoutRecord record;
20 |
21 | public double wattsSum, cadenceSum, speedSum, hrSum;
22 | public double distance;
23 | public double ftp = TrainApplication.getFTP();
24 | public int hertz, count;
25 |
26 | public int hrMax;
27 | public int speedMax;
28 | public int cadenceMax;
29 |
30 | public int lapBeep, lap;
31 | public boolean recording = false, cancelCalled = false;
32 | public boolean isNewLap = false;
33 | public long now = 0;
34 |
35 | public List lapsControl= new ArrayList<>();
36 |
37 | public ArrayList watts = new ArrayList<>(); // 1s samples [watts]
38 | public ArrayList hr = new ArrayList<>(); // 1s samples [bpm]
39 | public ArrayList speed = new ArrayList<>(); // 1s samples [km/h]
40 | public ArrayList cadence = new ArrayList<>(); // 1s samples [rpm]
41 |
42 | public Rolling watts30;
43 | private int recPerSec;
44 |
45 | public TrainState(WorkoutData workout, int recPerSec) {
46 | this.recPerSec = recPerSec;
47 | this.workout = workout;
48 | watts30 = new Rolling(recPerSec * 30);
49 | clear();
50 | }
51 |
52 | public void clear() {
53 | hertz = count = 0;
54 | wattsSum = hrSum = speedSum = cadenceSum = 0;
55 | distance = 0;
56 |
57 | // set initial
58 | cadenceMax = 600;
59 | hrMax = 220;
60 | speedMax = 50;
61 | lapBeep = 0;
62 |
63 | watts30.clear();
64 | watts.clear();
65 | hr.clear();
66 | speed.clear();
67 | cadence.clear();
68 |
69 | lapsControl.clear();
70 |
71 | for (LapPoint lap : workout.getErg().getLapPoints()) {
72 | lapsControl.add(new LapControl(recPerSec, lap.lapNum, lap.name, lap.x, lap.y));
73 | }
74 | }
75 |
76 | private void newLapInfo(long now, double hr, double cad, double watts, double speed, double distance) {
77 | for (LapControl lap : this.lapsControl) {
78 | if (lap.isRunning(now)) {
79 | lap.add();
80 | lap.setCadence(cad);
81 | lap.setHr(hr);
82 | lap.setWatts(watts);
83 | lap.setSpeed(speed);
84 | lap.setDistance(distance);
85 | }
86 | }
87 | }
88 |
89 | public LapControl getMainLap() {
90 | return lapsControl.get(0);
91 | }
92 |
93 | public LapControl getCurrentLap() {
94 | return this.lapsControl.get(lap);
95 | }
96 |
97 | public void workoutStart(File recordFile, long startTime) {
98 | }
99 |
100 | private void createInterval(LapControl avgLap) {
101 | }
102 |
103 | public void workoutComplete(File recordFile, long totalTicks,
104 | byte[] bitmap, byte[] bitmapMin) {
105 | LapControl workoutLap = this.lapsControl.get(0);
106 |
107 | for (LapControl lap : this.lapsControl) {
108 | if (lap.getCount() > 0)
109 | createInterval(lap);
110 | }
111 |
112 | Uri fileUri = FileProvider.getUriForFile(TrainApplication.getAppContext(),
113 | "com.trainerandroid.fileprovider", recordFile);
114 |
115 | }
116 |
117 | public void updateRealtimeData(RealtimeData rt) {
118 | count++;
119 | now = rt.getMsecs();
120 | distance = rt.getDistance();
121 | lap = rt.getLap();
122 |
123 | if (isNewLap) isNewLap = false;
124 |
125 | this.newLapInfo(now, rt.getHr(), rt.getCadence(), rt.getWatts(),
126 | rt.getSpeed(), rt.getDistance());
127 |
128 | watts30.add(rt.getWatts());
129 |
130 | //wbalSum += rt.getWbal();
131 | wattsSum += rt.getWatts();
132 | hrSum += rt.getHr();
133 | cadenceSum += rt.getCadence();
134 | speedSum += rt.getSpeed();
135 |
136 | hertz++;
137 |
138 | // did we get 5 samples (5hz refresh rate) ?
139 | if (hertz == recPerSec) {
140 | //int b = wbalSum / 5.0f;
141 | //wbal << b;
142 | int w = (int) (wattsSum / recPerSec);
143 | watts.add(w);
144 | int h = (int) (hrSum / recPerSec);
145 | hr.add(h);
146 | double s = (speedSum / recPerSec);
147 | speed.add(s);
148 | int c = (int) (cadenceSum / recPerSec);
149 | cadence.add(c);
150 |
151 | // clear for next time
152 | hertz = 0;
153 | wattsSum = hrSum = speedSum = cadenceSum = 0;
154 | }
155 | }
156 |
157 |
158 | }
159 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/workout_list_content.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
12 |
13 |
19 |
20 |
21 |
22 |
23 |
31 |
32 |
36 |
37 |
42 |
43 |
50 |
51 |
59 |
60 |
61 |
66 |
67 |
74 |
75 |
83 |
84 |
85 |
90 |
91 |
98 |
99 |
107 |
108 |
112 |
113 |
124 |
125 |
126 |
127 |
128 |
129 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/app/src/main/java/com/trainerdb/trainerandroid/data/ErgFile.java:
--------------------------------------------------------------------------------
1 | package com.trainerdb.trainerandroid.data;
2 |
3 | import android.graphics.PointF;
4 |
5 | import com.trainerdb.trainerandroid.TrainApplication;
6 |
7 | import java.io.Serializable;
8 | import java.util.ArrayList;
9 | import java.util.List;
10 |
11 | /**
12 | * Created by dcotrim on 22/06/2016.
13 | */
14 | public class ErgFile implements Serializable {
15 | private List coursePoints;
16 | private List textPoints;
17 | private List lapPoints;
18 | private long duration = 0;
19 |
20 | public long getDuration() {
21 | return duration;
22 | }
23 |
24 | public List getCoursePoints() {
25 | return coursePoints;
26 | }
27 |
28 | public List getLapPoints() {
29 | return lapPoints;
30 | }
31 |
32 | public List getTextPoints() {
33 | return textPoints;
34 | }
35 |
36 | public ErgFile(List points, List laps, List texts) {
37 | //List lines = new ArrayList();
38 | coursePoints = (points == null ? new ArrayList() : points);
39 | textPoints = (texts == null ? new ArrayList() : texts);;
40 | lapPoints = (laps == null ? new ArrayList() : laps);;
41 |
42 | duration = (long) coursePoints.get(coursePoints.size() - 1).t;
43 |
44 | int ftp = TrainApplication.getFTP();
45 | for (ErgPoint point: coursePoints) {
46 | point.w = (int) ((point.w / 100) * ftp);
47 | }
48 |
49 | Boolean newLapWorkout = false;
50 | if (this.lapPoints.size() == 0)
51 | newLapWorkout = true;
52 | else {
53 | LapPoint first = this.lapPoints.get(0);
54 | if (first.x != 0 || first.y != duration)
55 | newLapWorkout = true;
56 | }
57 | if (newLapWorkout)
58 | newLap(0, duration, 0, "Workout");
59 | }
60 |
61 | private void newLap(long x, long y, int num, String name) {
62 | LapPoint newLap = new LapPoint();
63 | newLap.x = x;
64 | newLap.y = y;
65 | newLap.name = name;
66 | newLap.lapNum = num;
67 | lapPoints.add(newLap);
68 | }
69 |
70 | public int lapAt(long x) {
71 | if (x < 0 || x > duration) return -100; // out of bounds!!!
72 |
73 | int lapNum = 0;
74 | long temp = 0;
75 | if (lapPoints.size() > 0) {
76 | for (int i = 0; i < lapPoints.size(); i++) {
77 | LapPoint lap = lapPoints.get(i);
78 | if (x >= lap.x && x <= lap.y) {
79 | if (lap.x > temp) {
80 | temp = lap.x;
81 | lapNum = lap.lapNum;
82 | }
83 | }
84 | }
85 | }
86 | return lapNum;
87 | }
88 |
89 | public String textAt(long x) {
90 | String text = "";
91 | if (x < 0 || x > duration) return "-1"; // out of bounds!!!
92 |
93 | long groupStart = -1;
94 | long start = 0;
95 | if (textPoints.size() > 0) {
96 | for (int i = 0; i < textPoints.size(); i++) {
97 | TextPoint tPoint = textPoints.get(i);
98 | if (groupStart != tPoint.x) {
99 | groupStart = tPoint.x;
100 | start = groupStart;
101 | } else
102 | start += tPoint.duration;
103 |
104 | if (x < start) continue;
105 | if (x > (start + (tPoint.duration))) continue;
106 |
107 | return tPoint.text;// + TrainApplication.formatTime(tPoint.duration + start - x);
108 | }
109 | }
110 | return text;
111 | }
112 |
113 | public double wattsAt(long x) {
114 | if (x < 0 || x > duration) return -100;
115 |
116 | int rightPoint, leftPoint;
117 | leftPoint = 0;
118 | rightPoint = 1;
119 |
120 | while (x < coursePoints.get(leftPoint).t || x > coursePoints.get(rightPoint).t) {
121 | if (x < coursePoints.get(leftPoint).t) {
122 | leftPoint--;
123 | rightPoint--;
124 | } else if (x > coursePoints.get(rightPoint).t) {
125 | leftPoint++;
126 | rightPoint++;
127 | }
128 | }
129 |
130 | if (coursePoints.get(leftPoint).w == coursePoints.get(rightPoint).w)
131 | return coursePoints.get(rightPoint).w;
132 |
133 | double deltaW = coursePoints.get(rightPoint).w - coursePoints.get(leftPoint).w;
134 | double deltaT = coursePoints.get(rightPoint).t - coursePoints.get(leftPoint).t;
135 | double offT = x - coursePoints.get(leftPoint).t;
136 | double factor = offT / deltaT;
137 |
138 | double nowW = coursePoints.get(leftPoint).w + (deltaW * factor);
139 |
140 | return nowW;
141 | }
142 |
143 | public long nextLapTime(long x) {
144 | //if (!isValid()) return -1; // not a valid ergfile
145 |
146 | long next = duration;
147 | long temp = 0;
148 | // do we need to return the Lap marker?
149 | if (lapPoints.size() > 0) {
150 | for (int i = 0; i < lapPoints.size(); i++) {
151 | LapPoint lap = lapPoints.get(i);
152 | if (x >= lap.x && x <= lap.y) {
153 | if (lap.x > temp) {
154 | temp = lap.x;
155 | next = lap.y;
156 | }
157 | }
158 | if (x < lap.x && lap.x < next) next = lap.x;
159 | if (x < lap.y && lap.y < next) next = lap.y;
160 | }
161 | }
162 | return next; // nope, no marker ahead of there
163 | }
164 | }
165 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/workout_detail.xml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
13 |
14 |
24 |
25 |
29 |
30 |
35 |
36 |
45 |
46 |
56 |
57 |
58 |
63 |
64 |
73 |
74 |
84 |
85 |
86 |
91 |
92 |
101 |
102 |
112 |
113 |
114 |
115 |
116 |
125 |
126 |
134 |
135 |
144 |
--------------------------------------------------------------------------------
/app/src/main/java/com/trainerdb/trainerandroid/listeners/AntSpeedDistance.java:
--------------------------------------------------------------------------------
1 | package com.trainerdb.trainerandroid.listeners;
2 |
3 | import com.dsi.ant.plugins.antplus.pcc.AntPlusBikeCadencePcc;
4 | import com.dsi.ant.plugins.antplus.pcc.AntPlusBikeSpeedDistancePcc;
5 | import com.dsi.ant.plugins.antplus.pcc.defines.DeviceState;
6 | import com.dsi.ant.plugins.antplus.pcc.defines.EventFlag;
7 | import com.dsi.ant.plugins.antplus.pcc.defines.RequestAccessResult;
8 | import com.dsi.ant.plugins.antplus.pccbase.AntPluginPcc;
9 | import com.trainerdb.trainerandroid.TrainApplication;
10 |
11 | import java.math.BigDecimal;
12 | import java.util.EnumSet;
13 |
14 | /**
15 | * Created by Daniel on 25/06/2016.
16 | */
17 | public class AntSpeedDistance extends Ant {
18 |
19 | public AntPluginPcc.IPluginAccessResultReceiver mResultReceiver;
20 |
21 | public AntSpeedDistance(int pDeviceNumber, final AntPlusController controller) {
22 | super(pDeviceNumber, controller);
23 |
24 | mResultReceiver = new AntPluginPcc.IPluginAccessResultReceiver() {
25 | //Handle the result, connecting to events on success or reporting failure to user.
26 | @Override
27 | public void onResultReceived(AntPlusBikeSpeedDistancePcc result, RequestAccessResult resultCode, DeviceState initialDeviceState) {
28 | controller.setDeviceStatus("AntSpeedDistance: " + resultCode);
29 | if (resultCode == com.dsi.ant.plugins.antplus.pcc.defines.RequestAccessResult.SUCCESS) {
30 | deviceNumber = result.getAntDeviceNumber();
31 |
32 | result.subscribeMotionAndSpeedDataEvent(
33 | new AntPlusBikeSpeedDistancePcc.IMotionAndSpeedDataReceiver() {
34 | @Override
35 | public void onNewMotionAndSpeedData(long l, EnumSet enumSet, boolean b) {
36 |
37 | }
38 | }
39 | );
40 | result.subscribeCalculatedSpeedEvent(
41 | new AntPlusBikeSpeedDistancePcc.CalculatedSpeedReceiver(
42 | new BigDecimal(TrainApplication.getWheelCircunference() / 1000)) {
43 | @Override
44 | public void onNewCalculatedSpeed(long estTimestamp, EnumSet eventFlags, final BigDecimal calculatedSpeed) {
45 | controller.setSpeed(calculatedSpeed.doubleValue() * 3.6);
46 | }
47 | }
48 | );
49 | result.subscribeRawSpeedAndDistanceDataEvent(
50 | new AntPlusBikeSpeedDistancePcc.IRawSpeedAndDistanceDataReceiver() {
51 | @Override
52 | public void onNewRawSpeedAndDistanceData(final long estTimestamp, final EnumSet eventFlags, final BigDecimal timestampOfLastEvent, final long cumulativeRevolutions) {
53 | //controller.setHr(0);
54 | }
55 | }
56 | );
57 |
58 | if (result.isSpeedAndCadenceCombinedSensor()) {
59 | AntPlusBikeCadencePcc.requestAccess(TrainApplication.getAppContext(), deviceNumber, 0, true,
60 | new AntPluginPcc.IPluginAccessResultReceiver() {
61 | @Override
62 | public void onResultReceived(AntPlusBikeCadencePcc resultCadence, RequestAccessResult resultCodeCadence, DeviceState initialDeviceStateCode) {
63 | if (resultCodeCadence == RequestAccessResult.SUCCESS) {
64 | resultCadence.subscribeCalculatedCadenceEvent(
65 | new AntPlusBikeCadencePcc.ICalculatedCadenceReceiver() {
66 | @Override
67 | public void onNewCalculatedCadence(long estTimestamp, EnumSet eventFlags, final BigDecimal calculatedCadence) {
68 | controller.setCadence(calculatedCadence.doubleValue());
69 | }
70 | }
71 | );
72 | }
73 | }
74 | }, new AntPluginPcc.IDeviceStateChangeReceiver() {
75 | @Override
76 | public void onDeviceStateChange(DeviceState deviceState) {
77 | if (deviceState.equals(DeviceState.DEAD)) {
78 | //zeroReadings();
79 | }
80 | }
81 | });
82 | }
83 |
84 |
85 | } else {
86 | if (resultCode == com.dsi.ant.plugins.antplus.pcc.defines.RequestAccessResult.SEARCH_TIMEOUT) {
87 | requestAccess();
88 | }
89 | }
90 | }
91 | };
92 |
93 |
94 | requestAccess();
95 | }
96 |
97 | @Override
98 | protected void requestAccess() {
99 | releaseHandle = AntPlusBikeSpeedDistancePcc.requestAccess(TrainApplication.getAppContext(), deviceNumber, 0, true, mResultReceiver, mDeviceStateChangeReceiver);
100 | }
101 |
102 | @Override
103 | protected void zeroReadings() {
104 | controller.setCadence(0);
105 | controller.setSpeed(0);
106 | }
107 | }
108 |
--------------------------------------------------------------------------------
/app/src/main/java/com/trainerdb/trainerandroid/TrainWorkoutView.java:
--------------------------------------------------------------------------------
1 | package com.trainerdb.trainerandroid;
2 |
3 | import android.content.Context;
4 | import android.graphics.Bitmap;
5 | import android.graphics.Canvas;
6 | import android.graphics.Color;
7 | import android.graphics.Paint;
8 | import android.graphics.PointF;
9 | import android.graphics.Rect;
10 | import android.graphics.RectF;
11 | import android.util.AttributeSet;
12 |
13 | import com.trainerdb.trainerandroid.train.TrainState;
14 |
15 | /**
16 | * Created by dcotrim on 23/06/2016.
17 | */
18 | public class TrainWorkoutView extends WorkoutView {
19 | private TrainState state;
20 | Paint pPower = new Paint();
21 | Paint pHr = new Paint();
22 | Paint pCadence = new Paint();
23 | Paint defaultPaint = new Paint();
24 | Paint pText = new Paint();
25 | Paint pSelectInterval = new Paint();
26 | Bitmap cachedBitmap;
27 | PointF lastPointWatts, lastPointHr, lastPointCadence;
28 | Rect srcZoom;
29 | int selectedInterval = 0;
30 |
31 | public enum Transform {
32 | WATTS, HR, CADENCE, SPEED
33 | }
34 |
35 | public TrainWorkoutView(Context context, AttributeSet attrs) {
36 | super(context, attrs);
37 | pCadence.setColor(Color.WHITE);
38 | pCadence.setStrokeWidth(5);
39 | pPower.setColor(Color.YELLOW);
40 | pPower.setStrokeWidth(5);
41 | pHr.setColor(Color.RED);
42 | pHr.setStrokeWidth(5);
43 | pText.setColor(Color.WHITE);
44 | pText.setTextAlign(Paint.Align.CENTER);
45 | pSelectInterval.setColor(Color.argb(64, 255, 255, 0));
46 | pSelectInterval.setStyle(Paint.Style.FILL);
47 |
48 | IHEIGHT = 10;
49 | THEIGHT = 35;
50 | BHEIGHT = 45;
51 | LWIDTH = 75;
52 | RWIDTH = 35;
53 | POWERSCALEWIDTH = 5;
54 | }
55 |
56 | public void setState(TrainState state) {
57 | this.state = state;
58 | this.workout = state.workout;
59 | this.ergs = workout.getErg().getCoursePoints();
60 | recompute();
61 | postInvalidate();
62 | }
63 |
64 | public Bitmap getCachedBitmap() {
65 | return cachedBitmap;
66 | }
67 |
68 | protected PointF transform(float seconds, float watts, Transform type) {
69 | RectF c = canvas();
70 |
71 | float xratio = c.width() / (maxVx - minVx);
72 | float yratio = c.height() / (maxY - minY);
73 |
74 | return new PointF(c.left - (minVx * xratio) + (seconds * xratio), c.bottom - (watts * yratio));
75 | }
76 |
77 | private void paintTelemetry(boolean firstDraw, Canvas canvas) {
78 | if (!firstDraw && !state.recording) return;
79 |
80 | long now = state.now;
81 |
82 | PointF here;
83 | if (state.watts.size() > 0) {
84 | int pos = state.watts.size() - 1;
85 | if (lastPointWatts == null) {
86 | lastPointWatts = transform(0, state.watts.get(0), Transform.WATTS);
87 | for (int i = 1; i <= pos; i++) {
88 | here = transform(i * 1000, state.watts.get(i), Transform.WATTS);
89 | canvas.drawLine(lastPointWatts.x, lastPointWatts.y, here.x, here.y, pPower);
90 | lastPointWatts = here;
91 | }
92 | } else {
93 | here = transform(now, state.watts.get(pos), Transform.WATTS);
94 | canvas.drawLine(lastPointWatts.x, lastPointWatts.y, here.x, here.y, pPower);
95 | lastPointWatts = here;
96 | }
97 | }
98 |
99 | if (state.cadence.size() > 1) {
100 | int pos = state.cadence.size() - 1;
101 | if (lastPointCadence == null) {
102 | lastPointCadence = transform(0, state.cadence.get(0), Transform.CADENCE);
103 | for (int i = 1; i <= pos; i++) {
104 | here = transform(i * 1000, state.cadence.get(i), Transform.CADENCE);
105 | canvas.drawLine(lastPointCadence.x, lastPointCadence.y, here.x, here.y, pCadence);
106 | lastPointCadence = here;
107 | }
108 | } else {
109 | here = transform(now, state.cadence.get(pos), Transform.CADENCE);
110 | canvas.drawLine(lastPointCadence.x, lastPointCadence.y, here.x, here.y, pCadence);
111 | lastPointCadence = here;
112 | }
113 | }
114 |
115 | if (state.hr.size() > 1) {
116 | int pos = state.hr.size() - 1;
117 | if (lastPointHr == null) {
118 | lastPointHr = transform(0, state.hr.get(0), Transform.HR);
119 | for (int i = 1; i <= pos; i++) {
120 | here = transform(i * 1000, state.hr.get(i), Transform.HR);
121 | canvas.drawLine(lastPointHr.x, lastPointHr.y, here.x, here.y, pHr);
122 | lastPointHr = here;
123 | }
124 | } else {
125 | here = transform(now, state.hr.get(pos), Transform.HR);
126 | canvas.drawLine(lastPointHr.x, lastPointHr.y, here.x, here.y, pHr);
127 | lastPointHr = here;
128 | }
129 | }
130 |
131 | }
132 |
133 | private void paintNow(Canvas canvas) {
134 | if (!state.recording) return;
135 |
136 | // get now
137 | float px = transform(state.now, 0).x;
138 |
139 | Paint linePen = new Paint();
140 | linePen.setColor(Color.YELLOW);
141 | linePen.setStrokeWidth(5);
142 |
143 | canvas.drawLine(px, canvas().top, px, canvas().bottom, linePen);
144 | }
145 |
146 | private void selectInterval(Canvas canvas) {
147 | if (selectedInterval != 0) {
148 |
149 | }
150 | }
151 |
152 | private Rect calcIntervalCanvas() {
153 | Rect rectInterval = new Rect();
154 | long timeLeft = state.now - (60 * 1000);
155 | long timeRight = state.now + (2 * 60 * 1000);
156 |
157 | float left = transform(timeLeft, 0).x;
158 | float right = transform(timeRight, 0).x;
159 |
160 | (new RectF(left, canvas().top, right, canvas().bottom)).round(rectInterval);
161 | return rectInterval;
162 | }
163 |
164 | private void doInitialDrawing(Canvas canvas) {
165 | }
166 |
167 | public void setSelectedInterval(int selectedInterval) {
168 | this.selectedInterval = selectedInterval;
169 | this.postInvalidate();
170 | }
171 |
172 | @Override
173 | protected void onDraw(Canvas canvas) {
174 | if (state == null) {
175 | super.onDraw(canvas);
176 | return;
177 | }
178 |
179 | //Rect srcFull = new Rect(0, 0, getWidth(), getHeight());
180 | //RectF rectInterval = new RectF(0, 0, getWidth(), getHeight() / 3);
181 | //RectF rectFull = new RectF(0, getHeight() / 3, getWidth(), getHeight());
182 |
183 | if (!initialDrawingIsPerformed) {
184 | lastPointWatts = lastPointCadence = lastPointHr = null;
185 | super.onDraw(canvas);
186 | this.cachedBitmap = Bitmap.createBitmap(getWidth(), getHeight(),
187 | Bitmap.Config.ARGB_8888); //Change to lower bitmap config if possible.
188 | Canvas cacheCanvas = new Canvas(this.cachedBitmap);
189 | super.onDraw(cacheCanvas);
190 | doInitialDrawing(cacheCanvas);
191 | paintTelemetry(true, cacheCanvas);
192 | canvas.drawBitmap(this.cachedBitmap, 0, 0, defaultPaint);
193 | //canvas.drawBitmap(this.cachedBitmap, srcFull, rectInterval, defaultPaint);
194 | //canvas.drawBitmap(this.cachedBitmap, srcFull, rectFull, defaultPaint);
195 | initialDrawingIsPerformed = true;
196 | } else {
197 | //calcIntervalCanvas();
198 | Canvas cacheCanvas = new Canvas(this.cachedBitmap);
199 | paintTelemetry(false, cacheCanvas);
200 | canvas.drawBitmap(this.cachedBitmap, 0, 0, defaultPaint);
201 | //canvas.drawBitmap(cachedBitmap, srcFull, rectFull, defaultPaint);
202 | //canvas.drawBitmap(cachedBitmap, calcIntervalCanvas(), rectInterval, defaultPaint);
203 | paintNow(canvas);
204 | selectInterval(canvas);
205 | }
206 | }
207 | }
208 |
--------------------------------------------------------------------------------
/app/src/main/java/com/trainerdb/trainerandroid/activities/TrainFileListActivity.java:
--------------------------------------------------------------------------------
1 | package com.trainerdb.trainerandroid.activities;
2 |
3 | import android.app.Activity;
4 | import android.content.Context;
5 | import android.content.DialogInterface;
6 | import android.content.Intent;
7 | import android.net.Uri;
8 | import android.os.Bundle;
9 | import android.support.design.widget.FloatingActionButton;
10 | import android.support.design.widget.Snackbar;
11 | import android.support.v4.content.FileProvider;
12 | import android.support.v7.app.ActionBar;
13 | import android.support.v7.app.AlertDialog;
14 | import android.support.v7.app.AppCompatActivity;
15 | import android.support.v7.widget.Toolbar;
16 | import android.view.LayoutInflater;
17 | import android.view.Menu;
18 | import android.view.MenuItem;
19 | import android.view.View;
20 | import android.view.ViewGroup;
21 | import android.widget.AdapterView;
22 | import android.widget.ArrayAdapter;
23 | import android.widget.CheckBox;
24 | import android.widget.ListView;
25 | import android.widget.TextView;
26 |
27 | import com.trainerdb.trainerandroid.R;
28 | import com.trainerdb.trainerandroid.train.TrainFile;
29 |
30 | import java.io.File;
31 | import java.util.ArrayList;
32 | import java.util.Iterator;
33 | import java.util.List;
34 |
35 | public class TrainFileListActivity extends AppCompatActivity {
36 |
37 | List files;
38 | ListView list;
39 | FileListAdapter adapter;
40 |
41 | @Override
42 | protected void onCreate(Bundle savedInstanceState) {
43 | super.onCreate(savedInstanceState);
44 | setContentView(R.layout.activity_train_file_list);
45 |
46 | Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar);
47 | setSupportActionBar(myToolbar);
48 | this.getSupportActionBar().setTitle("Files");
49 |
50 | // Show the Up button in the action bar.
51 | ActionBar actionBar = getSupportActionBar();
52 | if (actionBar != null) {
53 | actionBar.setDisplayHomeAsUpEnabled(true);
54 | }
55 |
56 | files = getListFiles(this.getFilesDir());
57 | files.addAll(getListFiles(this.getExternalFilesDir(null)));
58 |
59 | list = (ListView) findViewById(R.id.listFiles);
60 |
61 | adapter = new FileListAdapter(this, files);
62 | list.setAdapter(adapter);
63 |
64 | list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
65 | @Override
66 | public void onItemClick(AdapterView> parent, View view, int position, long id) {
67 | CheckBox chkSelected = (CheckBox) view.findViewById(R.id.checkBoxSelected);
68 | if (chkSelected.isChecked())
69 | files.get(position).setSelected(false);
70 | else
71 | files.get(position).setSelected(true);
72 |
73 | adapter.notifyDataSetChanged();
74 | }
75 | });
76 | }
77 |
78 | @Override
79 | public boolean onOptionsItemSelected(MenuItem item) {
80 | int id = item.getItemId();
81 | if (id == R.id.action_delete) {
82 | AlertDialog.Builder abuilder = new AlertDialog.Builder(this);
83 | abuilder.setMessage("Delete workout(s)?");
84 | abuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
85 | public void onClick(DialogInterface dialog, int which) {
86 | Iterator i = files.iterator();
87 | while (i.hasNext()) {
88 | TrainFile file = i.next();
89 | if (file.isSelected()) {
90 | file.getFile().delete();
91 | i.remove();
92 | }
93 | }
94 | adapter.notifyDataSetChanged();
95 | }
96 | });
97 | abuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
98 | public void onClick(DialogInterface dialog, int which) {
99 | }
100 | });
101 | AlertDialog alert = abuilder.create();
102 | alert.show();
103 | } else if (id == R.id.action_share) {
104 | shareFilesSelected();
105 | /*
106 | Iterator i = files.iterator();
107 | while (i.hasNext()) {
108 | TrainFile file = i.next();
109 | if (file.isSelected()) {
110 |
111 | final FirebaseDatabase database = FirebaseDatabase.getInstance();
112 | final DatabaseReference ref = database.getReference("workouts");
113 | ref.child("145788").addListenerForSingleValueEvent(new ValueEventListener() {
114 | @Override
115 | public void onDataChange(DataSnapshot dataSnapshot) {
116 | final Workout w = dataSnapshot.getValue(Workout.class);
117 | w.downloadData(new CompleteListener() {
118 | @Override
119 | public void onComplete(boolean success) {
120 | FileController controller = new FileController(w, files.get(0), 1);
121 | controller.exportRecord();
122 | }
123 | });
124 | }
125 |
126 | @Override
127 | public void onCancelled(DatabaseError databaseError) {
128 | }
129 | });
130 | }
131 | }
132 | */
133 | }
134 |
135 | return super.onOptionsItemSelected(item);
136 | }
137 |
138 | @Override
139 | public boolean onCreateOptionsMenu(Menu menu) {
140 | getMenuInflater().inflate(R.menu.toolbar_files, menu);
141 | return true;
142 | }
143 |
144 | private void shareFilesSelected() {
145 | Intent shareIntent = new Intent();
146 | shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
147 | shareIntent.setType("application/xml");
148 |
149 | ArrayList filesUri = new ArrayList();
150 | for (TrainFile file : files) {
151 | if (file.isSelected()) {
152 | filesUri.add(FileProvider.getUriForFile(this, "com.trainerandroid.fileprovider", file.getFile()));
153 | }
154 | }
155 |
156 | shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
157 | //shareIntent.setDataAndType(fileUri, getContentResolver().getType(fileUri));
158 | // Set the result
159 | this.setResult(Activity.RESULT_OK, shareIntent);
160 |
161 | shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, filesUri);
162 | startActivity(Intent.createChooser(shareIntent, "Share"));
163 | }
164 |
165 | private List getListFiles(File parentDir) {
166 | ArrayList inFiles = new ArrayList<>();
167 | File[] files = parentDir.listFiles();
168 | for (File file : files) {
169 | if (file.isDirectory()) {
170 | inFiles.addAll(getListFiles(file));
171 | } else {
172 | if (file.getName().endsWith(".csv")) {
173 | inFiles.add(new TrainFile(file));
174 | }
175 | }
176 | }
177 | return inFiles;
178 | }
179 |
180 | public class FileListAdapter extends ArrayAdapter {
181 | private Activity context;
182 | private List list;
183 |
184 | public FileListAdapter(Activity context, List objects) {
185 | super((Context) context, R.layout.train_file_list_content, objects);
186 | this.context = context;
187 | this.list = objects;
188 | }
189 |
190 | @Override
191 | public View getView(int position, View convertView, ViewGroup parent) {
192 | View rowView = convertView;
193 |
194 | if (rowView == null) {
195 | LayoutInflater inflater = this.context.getLayoutInflater();
196 | rowView = inflater.inflate(R.layout.train_file_list_content, null, true);
197 |
198 | }
199 | TextView txtTitle = (TextView) rowView.findViewById(R.id.tvFileName);
200 | CheckBox chkSelected = (CheckBox) rowView.findViewById(R.id.checkBoxSelected);
201 |
202 | TrainFile file = this.list.get(position);
203 | txtTitle.setText(file.getName());
204 | chkSelected.setChecked(file.isSelected());
205 |
206 | return rowView;
207 | }
208 |
209 | @Override
210 | public int getCount() {
211 | return list != null ? list.size() : 0;
212 | }
213 |
214 | @Override
215 | public TrainFile getItem(int position) {
216 | return list.get(position);
217 | }
218 | }
219 |
220 | }
221 |
--------------------------------------------------------------------------------
/app/src/main/java/com/trainerdb/trainerandroid/activities/WorkoutFilterActivity.java:
--------------------------------------------------------------------------------
1 | package com.trainerdb.trainerandroid.activities;
2 |
3 | import android.app.Activity;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.os.Parcelable;
7 | import android.support.v7.app.AppCompatActivity;
8 | import android.os.Bundle;
9 | import android.support.v7.widget.Toolbar;
10 | import android.view.LayoutInflater;
11 | import android.view.Menu;
12 | import android.view.MenuItem;
13 | import android.view.View;
14 | import android.view.ViewGroup;
15 | import android.widget.BaseExpandableListAdapter;
16 | import android.widget.CheckBox;
17 | import android.widget.ExpandableListView;
18 | import android.widget.TextView;
19 | import android.widget.ToggleButton;
20 |
21 | import com.trainerdb.trainerandroid.R;
22 |
23 | import java.util.List;
24 |
25 | public class WorkoutFilterActivity extends AppCompatActivity {
26 |
27 | WorkoutListOptions options;
28 |
29 | @Override
30 | protected void onCreate(Bundle savedInstanceState) {
31 | super.onCreate(savedInstanceState);
32 | setContentView(R.layout.activity_workout_filter);
33 |
34 | Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
35 | setSupportActionBar(toolbar);
36 | toolbar.setTitle(getTitle());
37 |
38 | if (savedInstanceState == null) {
39 | Intent intent = this.getIntent();
40 | this.options = intent.getParcelableExtra("options");
41 | } else {
42 | this.options = (WorkoutListOptions)savedInstanceState.getSerializable("options");
43 | }
44 | if (options == null) {
45 | options = new WorkoutListOptions();
46 | options.prepare();
47 | }
48 |
49 | final ExpandableListView expListView = (ExpandableListView) findViewById(R.id.listFilter);
50 | FilterListAdapter expListAdapter = new FilterListAdapter(this, options.getList());
51 | expListView.setAdapter(expListAdapter);
52 | }
53 |
54 | @Override
55 | public boolean onCreateOptionsMenu(Menu menu) {
56 | getMenuInflater().inflate(R.menu.toolbar_filter, menu);
57 | return true;
58 | }
59 |
60 | @Override
61 | public boolean onOptionsItemSelected(MenuItem item) {
62 | int id = item.getItemId();
63 | if (id == R.id.action_filter_apply) {
64 | Intent resultIntent = new Intent();
65 | resultIntent.putExtra("options", (Parcelable) options);
66 | setResult(Activity.RESULT_OK, resultIntent);
67 | finish();
68 | return true;
69 | } else if (id == R.id.action_filter_cancel) {
70 | finish();
71 | }
72 | return super.onOptionsItemSelected(item);
73 | }
74 |
75 | public class FilterListAdapter extends BaseExpandableListAdapter {
76 |
77 | private Activity context;
78 | private List list;
79 |
80 | public FilterListAdapter(Activity context, List listPlanCategories) {
81 | this.context = context;
82 | this.list = listPlanCategories;
83 | }
84 |
85 | public Object getChild(int groupPosition, int childPosition) {
86 | return list.get(groupPosition).filters.get(childPosition);
87 | }
88 |
89 | public long getChildId(int groupPosition, int childPosition) {
90 | return childPosition;
91 | }
92 |
93 | @Override
94 | public int getChildType(int groupPosition, int childPosition) {
95 | return ((WorkoutListParent)getGroup(groupPosition)).sort ? 1 : 0;
96 | }
97 |
98 | @Override
99 | public int getChildTypeCount(){
100 | return 2;
101 | }
102 |
103 | @Override
104 | public View getChildView(final int groupPosition, final int childPosition,
105 | boolean isLastChild, View convertView, ViewGroup parent) {
106 |
107 | final WorkoutListItem item = (WorkoutListItem) getChild(groupPosition, childPosition);
108 | final WorkoutListParent parentItem = (WorkoutListParent) getGroup(groupPosition);
109 |
110 | int type = getChildType(groupPosition, childPosition);
111 | if (type == 1) {
112 |
113 | if (convertView == null) {
114 | LayoutInflater inflater = context.getLayoutInflater();
115 | convertView = inflater.inflate(R.layout.workout_filter_sort_content, null);
116 | }
117 |
118 | TextView tvFilterItemName = (TextView) convertView.findViewById(R.id.tvSortName);
119 | final ToggleButton toggleSortAsc = (ToggleButton) convertView.findViewById(R.id.toggleSortAsc);
120 | final ToggleButton toggleSortDesc = (ToggleButton) convertView.findViewById(R.id.toggleSortDesc);
121 |
122 | toggleSortAsc.setTextOn(item.ascName);
123 | toggleSortAsc.setTextOff(item.ascName);
124 | toggleSortDesc.setTextOn(item.descName);
125 | toggleSortDesc.setTextOff(item.descName);
126 |
127 | toggleSortAsc.setChecked(item.active && item.asc);
128 | toggleSortDesc.setChecked(item.active && !item.asc);
129 |
130 | toggleSortAsc.setOnClickListener(new View.OnClickListener() {
131 | @Override
132 | public void onClick(View v) {
133 | if (toggleSortAsc.isChecked()) {
134 | toggleSortDesc.setChecked(false);
135 | item.active = true;
136 | item.asc = true;
137 | } else {
138 | item.active = false;
139 | }
140 | }
141 | });
142 |
143 | toggleSortDesc.setOnClickListener(new View.OnClickListener() {
144 | @Override
145 | public void onClick(View v) {
146 | if (toggleSortDesc.isChecked()) {
147 | toggleSortAsc.setChecked(false);
148 | item.active = true;
149 | item.asc = false;
150 | } else {
151 | item.active = false;
152 | }
153 | }
154 | });
155 |
156 | tvFilterItemName.setText(item.name);
157 | }
158 | else {
159 | if (convertView == null) {
160 | LayoutInflater inflater = context.getLayoutInflater();
161 | convertView = inflater.inflate(R.layout.workout_filter_list_content, null);
162 | }
163 | TextView tvFilterItemName = (TextView) convertView.findViewById(R.id.tvFilterItemName);
164 | final CheckBox checkFilterItem = (CheckBox) convertView.findViewById(R.id.checkFilterItem);
165 | checkFilterItem.setChecked(item.active);
166 | checkFilterItem.setOnClickListener(new View.OnClickListener() {
167 | @Override
168 | public void onClick(View v) {
169 | item.active = checkFilterItem.isChecked();
170 | }
171 | });
172 |
173 | tvFilterItemName.setText(item.name);
174 | }
175 | return convertView;
176 | }
177 |
178 | public int getChildrenCount(int groupPosition) {
179 | return list.get(groupPosition).filters.size(); // laptopCollections.get(laptops.get(groupPosition)).size();
180 | }
181 |
182 | public Object getGroup(int groupPosition) {
183 | return list.get(groupPosition);
184 | }
185 |
186 | public int getGroupCount() {
187 | return list.size();
188 | }
189 |
190 | public long getGroupId(int groupPosition) {
191 | return groupPosition;
192 | }
193 |
194 | public View getGroupView(int groupPosition, boolean isExpanded,
195 | View convertView, ViewGroup parent) {
196 | WorkoutListParent parentItem = (WorkoutListParent) getGroup(groupPosition);
197 | if (convertView == null) {
198 | LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
199 | convertView = infalInflater.inflate(R.layout.workout_filter_group, null);
200 | }
201 | TextView item = (TextView) convertView.findViewById(R.id.tvParentItemName);
202 | //item.setTypeface(null, Typeface.BOLD);
203 | item.setText(parentItem.name);
204 | return convertView;
205 | }
206 |
207 | public boolean hasStableIds() {
208 | return true;
209 | }
210 |
211 | public boolean isChildSelectable(int groupPosition, int childPosition) {
212 | return true;
213 | }
214 | }
215 |
216 | }
217 |
--------------------------------------------------------------------------------
/app/src/main/java/com/trainerdb/trainerandroid/AutoResizeTextView.java:
--------------------------------------------------------------------------------
1 | package com.trainerdb.trainerandroid;
2 |
3 | import android.annotation.TargetApi;
4 | import android.content.Context;
5 | import android.content.res.Resources;
6 | import android.graphics.RectF;
7 | import android.os.Build;
8 | import android.text.Layout.Alignment;
9 | import android.text.StaticLayout;
10 | import android.text.TextPaint;
11 | import android.util.AttributeSet;
12 | import android.util.SparseIntArray;
13 | import android.util.TypedValue;
14 | import android.widget.TextView;
15 |
16 | public class AutoResizeTextView extends TextView {
17 | private interface SizeTester {
18 | /**
19 | *
20 | * @param suggestedSize
21 | * Size of text to be tested
22 | * @param availableSpace
23 | * available space in which text must fit
24 | * @return an integer < 0 if after applying {@code suggestedSize} to
25 | * text, it takes less space than {@code availableSpace}, > 0
26 | * otherwise
27 | */
28 | public int onTestSize(int suggestedSize, RectF availableSpace);
29 | }
30 |
31 | private RectF mTextRect = new RectF();
32 |
33 | private RectF mAvailableSpaceRect;
34 |
35 | private SparseIntArray mTextCachedSizes;
36 |
37 | private TextPaint mPaint;
38 |
39 | private float mMaxTextSize;
40 |
41 | private float mSpacingMult = 1.0f;
42 |
43 | private float mSpacingAdd = 0.0f;
44 |
45 | private float mMinTextSize = 20;
46 |
47 | private int mWidthLimit;
48 |
49 | private static final int NO_LINE_LIMIT = -1;
50 | private int mMaxLines;
51 |
52 | private boolean mEnableSizeCache = true;
53 | private boolean mInitiallized;
54 |
55 | public AutoResizeTextView(Context context) {
56 | super(context);
57 | initialize();
58 | }
59 |
60 | public AutoResizeTextView(Context context, AttributeSet attrs) {
61 | super(context, attrs);
62 | initialize();
63 | }
64 |
65 | public AutoResizeTextView(Context context, AttributeSet attrs, int defStyle) {
66 | super(context, attrs, defStyle);
67 | initialize();
68 | }
69 |
70 | private void initialize() {
71 | mPaint = new TextPaint(getPaint());
72 | mMaxTextSize = getTextSize();
73 | mAvailableSpaceRect = new RectF();
74 | mTextCachedSizes = new SparseIntArray();
75 | if (mMaxLines == 0) {
76 | // no value was assigned during construction
77 | mMaxLines = NO_LINE_LIMIT;
78 | }
79 | mInitiallized = true;
80 | }
81 |
82 | @Override
83 | public void setText(final CharSequence text, BufferType type) {
84 | super.setText(text, type);
85 | adjustTextSize(text.toString());
86 | }
87 |
88 | @Override
89 | public void setTextSize(float size) {
90 | mMaxTextSize = size;
91 | mTextCachedSizes.clear();
92 | adjustTextSize(getText().toString());
93 | }
94 |
95 | @Override
96 | public void setMaxLines(int maxlines) {
97 | super.setMaxLines(maxlines);
98 | mMaxLines = maxlines;
99 | reAdjust();
100 | }
101 |
102 | public int getMaxLines() {
103 | return mMaxLines;
104 | }
105 |
106 | @Override
107 | public void setSingleLine() {
108 | super.setSingleLine();
109 | mMaxLines = 1;
110 | reAdjust();
111 | }
112 |
113 | @Override
114 | public void setSingleLine(boolean singleLine) {
115 | super.setSingleLine(singleLine);
116 | if (singleLine) {
117 | mMaxLines = 1;
118 | } else {
119 | mMaxLines = NO_LINE_LIMIT;
120 | }
121 | reAdjust();
122 | }
123 |
124 | @Override
125 | public void setLines(int lines) {
126 | super.setLines(lines);
127 | mMaxLines = lines;
128 | reAdjust();
129 | }
130 |
131 | @Override
132 | public void setTextSize(int unit, float size) {
133 | Context c = getContext();
134 | Resources r;
135 |
136 | if (c == null)
137 | r = Resources.getSystem();
138 | else
139 | r = c.getResources();
140 | mMaxTextSize = TypedValue.applyDimension(unit, size,
141 | r.getDisplayMetrics());
142 | mTextCachedSizes.clear();
143 | adjustTextSize(getText().toString());
144 | }
145 |
146 | @Override
147 | public void setLineSpacing(float add, float mult) {
148 | super.setLineSpacing(add, mult);
149 | mSpacingMult = mult;
150 | mSpacingAdd = add;
151 | }
152 |
153 | /**
154 | * Set the lower text size limit and invalidate the view
155 | *
156 | * @param minTextSize
157 | */
158 | public void setMinTextSize(float minTextSize) {
159 | mMinTextSize = minTextSize;
160 | reAdjust();
161 | }
162 |
163 | private void reAdjust() {
164 | adjustTextSize(getText().toString());
165 | }
166 |
167 | private void adjustTextSize(String string) {
168 | if (!mInitiallized) {
169 | return;
170 | }
171 | int startSize = (int) mMinTextSize;
172 | int heightLimit = getMeasuredHeight() - getCompoundPaddingBottom()
173 | - getCompoundPaddingTop();
174 | mWidthLimit = getMeasuredWidth() - getCompoundPaddingLeft()
175 | - getCompoundPaddingRight();
176 | mAvailableSpaceRect.right = mWidthLimit;
177 | mAvailableSpaceRect.bottom = heightLimit;
178 | super.setTextSize(
179 | TypedValue.COMPLEX_UNIT_PX,
180 | efficientTextSizeSearch(startSize, (int) mMaxTextSize,
181 | mSizeTester, mAvailableSpaceRect));
182 | }
183 |
184 | private final SizeTester mSizeTester = new SizeTester() {
185 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
186 | @Override
187 | public int onTestSize(int suggestedSize, RectF availableSPace) {
188 | mPaint.setTextSize(suggestedSize);
189 | String text = getText().toString();
190 | boolean singleline = getMaxLines() == 1;
191 | if (singleline) {
192 | mTextRect.bottom = mPaint.getFontSpacing();
193 | mTextRect.right = mPaint.measureText(text);
194 | } else {
195 | StaticLayout layout = new StaticLayout(text, mPaint,
196 | mWidthLimit, Alignment.ALIGN_NORMAL, mSpacingMult,
197 | mSpacingAdd, true);
198 | // return early if we have more lines
199 | if (getMaxLines() != NO_LINE_LIMIT
200 | && layout.getLineCount() > getMaxLines()) {
201 | return 1;
202 | }
203 | mTextRect.bottom = layout.getHeight();
204 | int maxWidth = -1;
205 | for (int i = 0; i < layout.getLineCount(); i++) {
206 | if (maxWidth < layout.getLineWidth(i)) {
207 | maxWidth = (int) layout.getLineWidth(i);
208 | }
209 | }
210 | mTextRect.right = maxWidth;
211 | }
212 |
213 | mTextRect.offsetTo(0, 0);
214 | if (availableSPace.contains(mTextRect)) {
215 | // may be too small, don't worry we will find the best match
216 | return -1;
217 | } else {
218 | // too big
219 | return 1;
220 | }
221 | }
222 | };
223 |
224 | /**
225 | * Enables or disables size caching, enabling it will improve performance
226 | * where you are animating a value inside TextView. This stores the font
227 | * size against getText().length() Be careful though while enabling it as 0
228 | * takes more space than 1 on some fonts and so on.
229 | *
230 | * @param enable
231 | * enable font size caching
232 | */
233 | public void enableSizeCache(boolean enable) {
234 | mEnableSizeCache = enable;
235 | mTextCachedSizes.clear();
236 | adjustTextSize(getText().toString());
237 | }
238 |
239 | private int efficientTextSizeSearch(int start, int end,
240 | SizeTester sizeTester, RectF availableSpace) {
241 | if (!mEnableSizeCache) {
242 | return binarySearch(start, end, sizeTester, availableSpace);
243 | }
244 | String text = getText().toString();
245 | int key = text == null ? 0 : text.length();
246 | int size = mTextCachedSizes.get(key);
247 | if (size != 0) {
248 | return size;
249 | }
250 | size = binarySearch(start, end, sizeTester, availableSpace);
251 | mTextCachedSizes.put(key, size);
252 | return size;
253 | }
254 |
255 | private static int binarySearch(int start, int end, SizeTester sizeTester,
256 | RectF availableSpace) {
257 | int lastBest = start;
258 | int lo = start;
259 | int hi = end - 1;
260 | int mid = 0;
261 | while (lo <= hi) {
262 | mid = (lo + hi) >>> 1;
263 | int midValCmp = sizeTester.onTestSize(mid, availableSpace);
264 | if (midValCmp < 0) {
265 | lastBest = lo;
266 | lo = mid + 1;
267 | } else if (midValCmp > 0) {
268 | hi = mid - 1;
269 | lastBest = hi;
270 | } else {
271 | return mid;
272 | }
273 | }
274 | // make sure to return last best
275 | // this is what should always be returned
276 | return lastBest;
277 |
278 | }
279 |
280 | @Override
281 | protected void onTextChanged(final CharSequence text, final int start,
282 | final int before, final int after) {
283 | super.onTextChanged(text, start, before, after);
284 | reAdjust();
285 | }
286 |
287 | @Override
288 | protected void onSizeChanged(int width, int height, int oldwidth,
289 | int oldheight) {
290 | mTextCachedSizes.clear();
291 | super.onSizeChanged(width, height, oldwidth, oldheight);
292 | if (width != oldwidth || height != oldheight) {
293 | reAdjust();
294 | }
295 | }
296 | }
297 |
--------------------------------------------------------------------------------