close
The Wayback Machine - https://web.archive.org/web/20160329220721/https://developer.android.com/samples/SpeedTracker/Application/src/com.example.android.wearable.speedtracker/PhoneMainActivity.html
Show navigation Hide navigation
SpeedTracker / Application / src / com.example.android.wearable.speedtracker /

PhoneMainActivity.java

1
/*
2
 * Copyright (C) 2014 Google Inc. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
 
17
package com.example.android.wearable.speedtracker;
18
 
19
import com.google.android.gms.maps.CameraUpdateFactory;
20
import com.google.android.gms.maps.GoogleMap;
21
import com.google.android.gms.maps.MapFragment;
22
import com.google.android.gms.maps.model.LatLng;
23
import com.google.android.gms.maps.model.LatLngBounds;
24
import com.google.android.gms.maps.model.PolylineOptions;
25
 
26
import android.app.DatePickerDialog;
27
import android.os.AsyncTask;
28
import android.os.Bundle;
29
import android.support.v7.app.AppCompatActivity;
30
import android.text.format.DateUtils;
31
import android.util.Log;
32
import android.view.View;
33
import android.widget.DatePicker;
34
import android.widget.TextView;
35
 
36
import com.example.android.wearable.speedtracker.common.LocationEntry;
37
 
38
import java.util.ArrayList;
39
import java.util.Calendar;
40
import java.util.List;
41
 
42
/**
43
 * The main activity for the handset application. When a watch device reconnects to the handset
44
 * app, the collected GPS data on the watch, if any, is synced up and user can see his/her track on
45
 * a map. This data is then saved into an internal database and the corresponding data items are
46
 * deleted.
47
 */
48
public class PhoneMainActivity extends AppCompatActivity implements
49
        DatePickerDialog.OnDateSetListener {
50
 
51
    private static final String TAG = "PhoneMainActivity";
52
    private static final int BOUNDING_BOX_PADDING_PX = 50;
53
    private TextView mSelectedDateText;
54
    private GoogleMap mMap;
55
 
56
    @Override
57
    protected void onCreate(Bundle savedInstanceState) {
58
        super.onCreate(savedInstanceState);
59
        setContentView(R.layout.main_activity);
60
        mSelectedDateText = (TextView) findViewById(R.id.selected_date);
61
        mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
62
    }
63
 
64
    public void onClick(View view) {
65
        Calendar calendar = Calendar.getInstance();
66
        calendar.setTimeInMillis(System.currentTimeMillis());
67
        new DatePickerDialog(PhoneMainActivity.this, PhoneMainActivity.this,
68
                calendar.get(Calendar.YEAR),
69
                calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
70
    }
71
 
72
    @Override
73
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
74
        // the following if-clause is to get around a bug that causes this callback to be called
75
        // twice
76
        if (view.isShown()) {
77
            Calendar calendar = Calendar.getInstance();
78
            calendar.setTimeInMillis(System.currentTimeMillis());
79
            calendar.set(Calendar.YEAR, year);
80
            calendar.set(Calendar.MONTH, monthOfYear);
81
            calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
82
            String date = DateUtils.formatDateTime(this, calendar.getTimeInMillis(),
83
                    DateUtils.FORMAT_SHOW_DATE);
84
            mSelectedDateText.setText(getString(R.string.showing_for_date, date));
85
            showTrack(calendar);
86
        }
87
    }
88
 
89
    /**
90
     * An {@link android.os.AsyncTask} that is responsible for getting a list of {@link
91
     * com.example.android.wearable.speedtracker.common.LocationEntry} objects for a given day and
92
     * building a track from those points. In addition, it sets the smallest bounding box for the
93
     * map that covers all the points on the track.
94
     */
95
    private void showTrack(Calendar calendar) {
96
        new AsyncTask<Calendar, Void, Void>() {
97
 
98
            private List<LatLng> coordinates;
99
            private LatLngBounds bounds;
100
 
101
            @Override
102
            protected Void doInBackground(Calendar... params) {
103
                LocationDataManager dataManager = ((PhoneApplication) getApplicationContext())
104
                        .getDataManager();
105
                List<LocationEntry> entries = dataManager.getPoints(params[0]);
106
                if (entries != null && !entries.isEmpty()) {
107
                    coordinates = new ArrayList<LatLng>();
108
                    LatLngBounds.Builder builder = new LatLngBounds.Builder();
109
                    for (LocationEntry entry : entries) {
110
                        LatLng latLng = new LatLng(entry.latitude, entry.longitude);
111
                        builder.include(latLng);
112
                        coordinates.add(latLng);
113
                    }
114
                    bounds = builder.build();
115
                }
116
                return null;
117
            }
118
 
119
            @Override
120
            protected void onPostExecute(Void aVoid) {
121
                mMap.clear();
122
                if (coordinates == null || coordinates.isEmpty()) {
123
                    if (Log.isLoggable(TAG, Log.DEBUG)) {
124
                        Log.d(TAG, "No Entries found for that date");
125
                    }
126
                } else {
127
                    mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds,
128
                            BOUNDING_BOX_PADDING_PX));
129
                    mMap.addPolyline(new PolylineOptions().geodesic(true).addAll(coordinates));
130
                }
131
            }
132
 
133
        }.execute(calendar);
134
    }
135
}