-
Notifications
You must be signed in to change notification settings - Fork 15
/
MainActivity.java
233 lines (191 loc) · 6.97 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package mil.nga.mapcache;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.view.MenuItem;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import com.ipaulpro.afilechooser.utils.FileUtils;
import org.piwik.sdk.Piwik;
import org.piwik.sdk.Tracker;
import org.piwik.sdk.TrackerConfig;
import org.piwik.sdk.extra.TrackHelper;
import mil.nga.mapcache.io.MapCacheFileUtils;
//import org.matomo.sdk.Matomo;
//import org.matomo.sdk.TrackMe;
//import org.matomo.sdk.Tracker;
//import org.matomo.sdk.TrackerBuilder;
//import org.matomo.sdk.extra.MatomoApplication;
//import org.matomo.sdk.extra.TrackHelper;
/**
* Main Activity
*
* @author osbornb
*/
public class MainActivity extends AppCompatActivity {
/**
* Map permissions request code for accessing fine locations
*/
public static final int MAP_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 100;
/**
* Manager permissions request code for importing a GeoPackage as an external link
*/
public static final int MANAGER_PERMISSIONS_REQUEST_ACCESS_IMPORT_EXTERNAL = 200;
/**
* Manager permissions request code for reading / writing to GeoPackages already externally linked
*/
public static final int MANAGER_PERMISSIONS_REQUEST_ACCESS_EXISTING_EXTERNAL = 201;
/**
* Manager permissions request code for exporting a GeoPackage to external storage
*/
public static final int MANAGER_PERMISSIONS_REQUEST_ACCESS_EXPORT_DATABASE = 202;
/**
* Used to store the last screen title. For use in
* {@link #hideActionBar()}.
*/
private CharSequence title;
/**
* Map fragment
*/
private GeoPackageMapFragment mapFragment;
/**
* Manager fragment
*/
// private GeoPackageManagerFragment managerFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the content view
setContentView(R.layout.activity_main);
// Retrieve the fragments
// managerFragment = (GeoPackageManagerFragment) getSupportFragmentManager()
// .findFragmentById(R.id.fragment_manager);
mapFragment = (GeoPackageMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.fragment_map);
// Set up the map fragment
createMainFragment();
hideActionBar();
// Handle opening and importing GeoPackages
Intent intent = getIntent();
Uri uri = intent.getData();
if(uri == null){
Bundle bundle = intent.getExtras();
if(bundle != null){
Object objectUri = bundle.get(Intent.EXTRA_STREAM);
if(objectUri != null){
uri = (Uri)objectUri;
}
}
}
if (uri != null) {
handleIntentUri(uri, intent);
}
/**
* Use Matomo to track when users open the app
*/
String siteUrl = getString(R.string.matomo_url);
int siteId = getResources().getInteger(R.integer.matomo_site_id);
Tracker piwik = Piwik.getInstance(getApplicationContext()).newTracker(new TrackerConfig(siteUrl, siteId, "MapCacheTracker"));
String androidId = Settings.Secure.getString(getBaseContext().getContentResolver(),
Settings.Secure.ANDROID_ID);
TrackHelper.track().screen("/Main Activity").title("App Opened").with(piwik);
piwik.dispatch();
}
/**
* Set up the map fragment
*/
private void createMainFragment(){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.show(mapFragment);
title = getString(R.string.title_map);
transaction.commit();
}
/**
* Handle the URI from an intent for opening or importing a GeoPackage
*
* @param uri
*/
private void handleIntentUri(final Uri uri, Intent intent) {
String path = FileUtils.getPath(this, uri);
String name = MapCacheFileUtils.getDisplayName(this, uri, path);
try {
if (path != null) {
// managerFragment.importGeoPackageExternalLinkWithPermissions(name, uri, path);
mapFragment.startImportTaskWithPermissions(name, uri, path, intent);
} else {
// managerFragment.importGeoPackage(name, uri, path);
mapFragment.startImportTask(name, uri, path, intent);
}
} catch (final Exception e) {
try {
runOnUiThread(
new Runnable() {
@Override
public void run() {
GeoPackageUtils.showMessage(MainActivity.this,
"Open GeoPackage",
"Could not open file as a GeoPackage"
+ "\n\n"
+ e.getMessage());
}
});
} catch (Exception e2) {
// eat
}
}
}
/**
* {@inheritDoc}
*/
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
/**
* Hide the action bar
*/
public void hideActionBar() {
ActionBar actionBar = getSupportActionBar();
// actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
// actionBar.setDisplayShowTitleEnabled(true);
// actionBar.setTitle(title);
actionBar.hide();
}
/**
* {@inheritDoc}
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mapFragment.handleMenuClick(item)) {
return true;
}
// if (managerFragment.handleMenuClick(item)) {
// return true;
// }
return super.onOptionsItemSelected(item);
}
/**
* {@inheritDoc}
*/
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
// Check if permission was granted
boolean granted = grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED;
switch(requestCode) {
case MAP_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION:
mapFragment.setMyLocationEnabled();
break;
case MANAGER_PERMISSIONS_REQUEST_ACCESS_IMPORT_EXTERNAL:
mapFragment.importGeopackageFromFile();
break;
case MANAGER_PERMISSIONS_REQUEST_ACCESS_EXISTING_EXTERNAL:
// managerFragment.update(granted);
break;
}
}
}