Skip to content

Most visited

Recently visited

navigation
XYZTouristAttractions / Application / src / com.example.android.xyztouristattractions / ui /

AttractionListActivity.java

1
/*
2
 * Copyright 2015 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.xyztouristattractions.ui;
18
 
19
import android.Manifest;
20
import android.content.pm.PackageManager;
21
import android.os.Bundle;
22
import android.support.design.widget.Snackbar;
23
import android.support.v4.app.ActivityCompat;
24
import android.support.v7.app.AlertDialog;
25
import android.support.v7.app.AppCompatActivity;
26
import android.view.Menu;
27
import android.view.MenuItem;
28
import android.view.View;
29
import android.widget.Toast;
30
 
31
import com.example.android.xyztouristattractions.R;
32
import com.example.android.xyztouristattractions.common.Utils;
33
import com.example.android.xyztouristattractions.service.UtilityService;
34
 
35
/**
36
 * The main tourist attraction activity screen which contains a list of
37
 * attractions sorted by distance.
38
 */
39
public class AttractionListActivity extends AppCompatActivity implements
40
        ActivityCompat.OnRequestPermissionsResultCallback {
41
 
42
    private static final int PERMISSION_REQ = 0;
43
 
44
    @Override
45
    protected void onCreate(Bundle savedInstanceState) {
46
        super.onCreate(savedInstanceState);
47
        setContentView(R.layout.activity_main);
48
 
49
        if (savedInstanceState == null) {
50
            getSupportFragmentManager().beginTransaction()
51
                    .add(R.id.container, new AttractionListFragment())
52
                    .commit();
53
        }
54
 
55
        // Check fine location permission has been granted
56
        if (!Utils.checkFineLocationPermission(this)) {
57
            // See if user has denied permission in the past
58
            if (ActivityCompat.shouldShowRequestPermissionRationale(
59
                    this, Manifest.permission.ACCESS_FINE_LOCATION)) {
60
                // Show a simple snackbar explaining the request instead
61
                showPermissionSnackbar();
62
            } else {
63
                // Otherwise request permission from user
64
                if (savedInstanceState == null) {
65
                    requestFineLocationPermission();
66
                }
67
            }
68
        } else {
69
            // Otherwise permission is granted (which is always the case on pre-M devices)
70
            fineLocationPermissionGranted();
71
        }
72
    }
73
 
74
    @Override
75
    protected void onResume() {
76
        super.onResume();
77
        UtilityService.requestLocation(this);
78
    }
79
 
80
    @Override
81
    public boolean onCreateOptionsMenu(Menu menu) {
82
        // Inflate the menu; this adds items to the action bar if it is present.
83
        getMenuInflater().inflate(R.menu.main, menu);
84
        return true;
85
    }
86
 
87
    @Override
88
    public boolean onOptionsItemSelected(MenuItem item) {
89
        // Handle action bar item clicks here. The action bar will
90
        // automatically handle clicks on the Home/Up button, so long
91
        // as you specify a parent activity in AndroidManifest.xml.
92
        switch (item.getItemId()) {
93
            case R.id.test_notification:
94
                UtilityService.triggerWearTest(this, false);
95
                showDebugDialog(R.string.action_test_notification,
96
                        R.string.action_test_notification_dialog);
97
                return true;
98
            case R.id.test_microapp:
99
                UtilityService.triggerWearTest(this, true);
100
                showDebugDialog(R.string.action_test_microapp,
101
                        R.string.action_test_microapp_dialog);
102
                return true;
103
            case R.id.test_toggle_geofence:
104
                boolean geofenceEnabled = Utils.getGeofenceEnabled(this);
105
                Utils.storeGeofenceEnabled(this, !geofenceEnabled);
106
                Toast.makeText(this, geofenceEnabled ?
107
                        "Debug: Geofencing trigger disabled" :
108
                        "Debug: Geofencing trigger enabled", Toast.LENGTH_SHORT).show();
109
                return true;
110
        }
111
        return super.onOptionsItemSelected(item);
112
    }
113
 
114
    /**
115
     * Permissions request result callback
116
     */
117
    @Override
118
    public void onRequestPermissionsResult(
119
            int requestCode, String[] permissions, int[] grantResults) {
120
        switch (requestCode) {
121
            case PERMISSION_REQ:
122
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
123
                    fineLocationPermissionGranted();
124
                }
125
        }
126
    }
127
 
128
    /**
129
     * Request the fine location permission from the user
130
     */
131
    private void requestFineLocationPermission() {
132
        ActivityCompat.requestPermissions(this,
133
                new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_REQ);
134
    }
135
 
136
    /**
137
     * Run when fine location permission has been granted
138
     */
139
    private void fineLocationPermissionGranted() {
140
        UtilityService.addGeofences(this);
141
        UtilityService.requestLocation(this);
142
    }
143
 
144
    /**
145
     * Show a permission explanation snackbar
146
     */
147
    private void showPermissionSnackbar() {
148
        Snackbar.make(
149
                findViewById(R.id.container), R.string.permission_explanation, Snackbar.LENGTH_LONG)
150
                .setAction(R.string.permission_explanation_action, new View.OnClickListener() {
151
                    @Override
152
                    public void onClick(View v) {
153
                        requestFineLocationPermission();
154
                    }
155
                })
156
                .show();
157
    }
158
 
159
    /**
160
     * Show a basic debug dialog to provide more info on the built-in debug
161
     * options.
162
     */
163
    private void showDebugDialog(int titleResId, int bodyResId) {
164
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
165
                .setTitle(titleResId)
166
                .setMessage(bodyResId)
167
                .setPositiveButton(android.R.string.ok, null);
168
        builder.create().show();
169
    }
170
}
This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!