-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWeatherActivity.java
398 lines (315 loc) · 13.8 KB
/
WeatherActivity.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package com.arifulislam.tourguidepro.weather;
import android.Manifest;
import android.app.SearchManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.provider.SearchRecentSuggestions;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import com.arifulislam.tourguidepro.R;
import com.arifulislam.tourguidepro.events.EventActivity;
import com.arifulislam.tourguidepro.locations.MapsActivity;
import com.arifulislam.tourguidepro.services.LocationServiceHelper;
import com.google.firebase.auth.FirebaseAuth;
public class WeatherActivity extends AppCompatActivity implements
LocationServiceHelper.LocationResponseListener{
SharedPreferences preferences;
FirebaseAuth auth;
private Context context;
public static double latitude;
public static double longitude;
public static String UNIT_SELECTED;
public static int FORECAST_LIMIT_SELECTED;
public static boolean getWeatherUsingLatLon;
TabLayout tabLayout;
ViewPager viewPager;
TabPagerAdapter pagerAdapter;
String searchQuery;
private AlertDialog alertDialogUnits;
private AlertDialog alertDialogForecastList;
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
private LocationServiceHelper locationServiceHelper;
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_weather);
toolbar = (Toolbar) findViewById(R.id.toolbar);
context = this;
preferences = getApplicationContext().getSharedPreferences("user", MODE_PRIVATE);
editor = preferences.edit();
auth = FirebaseAuth.getInstance();
Intent intent = getIntent();
if (intent.ACTION_SEARCH.equals(intent.getAction())) {
searchQuery = intent.getStringExtra(SearchManager.QUERY);
// Toast.makeText(this, searchQuery, Toast.LENGTH_SHORT).show();
SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
RecentSearchSuggestions.AUTHORITY, RecentSearchSuggestions.MODE);
suggestions.saveRecentQuery(searchQuery, null);
getWeatherUsingLatLon = false;
}
initViews();
// initializing shared preferences
sharedPreferences = getSharedPreferences("WeatherSettings", MODE_PRIVATE);
editor = sharedPreferences.edit();
// initializing LocationHelper
locationServiceHelper = new LocationServiceHelper(this, this);
// getting data from sharedPreference
getSettingsData();
// setting up tabLayout and viewPager
setupTabPager();
//activateLocation();
/*DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);*/
}
private void getSettingsData() {
UNIT_SELECTED = sharedPreferences.getString("unit", WeatherServiceHelper.UNIT_METRIC);
FORECAST_LIMIT_SELECTED = sharedPreferences.getInt("limit", 7);
}
/**
* This method binds the pagerAdapter with tabLayout and viewPager
*/
private void setupTabPager() {
tabLayout.addTab(tabLayout.newTab().setText("Current Weather"));
tabLayout.addTab(tabLayout.newTab().setText("Forecast Weather"));
pagerAdapter = new TabPagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(pagerAdapter);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
}
private void initViews() {
tabLayout = findViewById(R.id.tabLayout);
viewPager = findViewById(R.id.viewPager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.weather_menu, menu);
/*SearchManager manager = (SearchManager) getSystemService(SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.item_search).getActionView();
if(manager !=null) {
// searchView.setSearchableInfo(manager.getSearchableInfo(getComponentName()));
searchView.setSubmitButtonEnabled(true);
}*/
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item_unit:
AlertDialogForUnitChange();
return true;
case R.id.item_forecast_limit:
AlertDialogForForecastLimit();
return true;
case R.id.event:
Intent n = new Intent(WeatherActivity.this, EventActivity.class);
startActivity(n);
case R.id.mapsAc:
Intent m = new Intent(WeatherActivity.this, MapsActivity.class);
startActivity(m);
default:
return super.onOptionsItemSelected(item);
}
}
private void AlertDialogForForecastLimit() {
CharSequence[] values = {" 7 Days ", " 14 Days "};
int checkedItem = -1;
if (FORECAST_LIMIT_SELECTED == 7) {
checkedItem = 0;
} else if (FORECAST_LIMIT_SELECTED == 14)
checkedItem = 1;
AlertDialog.Builder builder = new AlertDialog.Builder(WeatherActivity.this);
builder.setTitle("Select Your Choice");
builder.setSingleChoiceItems(values, checkedItem, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
switch (item) {
case 0:
FORECAST_LIMIT_SELECTED = 7;
editor.putInt("limit", 7);
editor.commit();
Toast.makeText(WeatherActivity.this, "Forecast Limit changed to 7 days", Toast
.LENGTH_LONG).show();
restartActivity();
break;
case 1:
FORECAST_LIMIT_SELECTED = 14;
editor.putInt("limit", 14);
editor.commit();
Toast.makeText(WeatherActivity.this, "Forecast Limit changed to 14 days", Toast
.LENGTH_LONG).show();
restartActivity();
break;
}
alertDialogForecastList.dismiss();
}
});
alertDialogForecastList = builder.create();
alertDialogForecastList.show();
}
private void restartActivity() {
Intent intent = getIntent();
finish();
startActivity(intent);
}
private void AlertDialogForUnitChange() {
int checkedItem = -1;
if (UNIT_SELECTED.equals(WeatherServiceHelper.UNIT_METRIC)) {
checkedItem = 0;
} else if (UNIT_SELECTED.equals(WeatherServiceHelper.UNIT_IMPERIAL))
checkedItem = 1;
CharSequence[] values = {" Metric ", " Imperial "};
AlertDialog.Builder builder = new AlertDialog.Builder(WeatherActivity.this);
builder.setTitle("Select Your Choice");
builder.setSingleChoiceItems(values, checkedItem, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
switch (item) {
case 0:
UNIT_SELECTED = WeatherServiceHelper.UNIT_METRIC;
editor.putString("unit", WeatherServiceHelper.UNIT_METRIC);
editor.commit();
Toast.makeText(WeatherActivity.this, "Unit Changed To Metric", Toast
.LENGTH_LONG).show();
restartActivity();
break;
case 1:
UNIT_SELECTED = WeatherServiceHelper.UNIT_IMPERIAL;
editor.putString("unit", WeatherServiceHelper.UNIT_IMPERIAL);
editor.commit();
Toast.makeText(WeatherActivity.this, "Unit Changed To Imperial", Toast
.LENGTH_LONG).show();
restartActivity();
break;
}
alertDialogUnits.dismiss();
}
});
alertDialogUnits = builder.create();
alertDialogUnits.show();
}
public void activateLocation() {
getWeatherUsingLatLon = true;
latitude = 0;
longitude = 0;
checkLocationPermission();
//get location latitude and longitude
locationServiceHelper.getLatLon();
}
private void checkLocationPermission() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission
.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,}, URLs
.PERMISSION_CODE_LOCATION);
return;
}
}
/* @SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_locations) {
Intent intent = new Intent(WeatherActivity.this, MapsActivity.class);
startActivity(intent);
} else if (id == R.id.nav_event) {
Intent intent = new Intent(this,EventActivity.class);
startActivity(intent);
} else if (id == R.id.nav_weather) {
//Intent intent = new Intent(this,WeatherActivity.class);
} else if (id == R.id.nav_nearby) {
Intent intent = new Intent(context,NearbyPlacesActivity.class);
finish();
startActivity(intent);
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_sign_out) {
auth.signOut();
editor.clear();
editor.putBoolean("signedIn",false);
editor.commit();
Toast.makeText(context,"Logged out",Toast.LENGTH_LONG).show();
Intent intent = new Intent(context,LoginActivity.class);
finish();
startActivity(intent);
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}*/
@Override
public void onLatLonReceived(double lat, double lon) {
// saving location to SharedPreference
editor.putFloat("lat", (float) lat);
editor.putFloat("lon", (float) lon);
editor.commit();
latitude = lat;
longitude = lon;
locationServiceHelper.stopLocationUpdates();
Intent intent = getIntent();
if (intent.ACTION_SEARCH.equals(intent.getAction()))
intent.setAction(Intent.ACTION_MAIN);
finish();
startActivity(intent);
}
public class TabPagerAdapter extends FragmentPagerAdapter {
int tabCount;
TabPagerAdapter(FragmentManager fm, int tabCount) {
super(fm);
this.tabCount = tabCount;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return CurrentWeatherFragment.getInstance(searchQuery);
case 1:
return ForecastWeatherFragment.getInstance();
}
return null;
}
@Override
public int getCount() {
return tabCount;
}
}
}