-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNavDrawerActivity.java
363 lines (326 loc) · 15.6 KB
/
NavDrawerActivity.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
package com.jrschugel.loadmanager;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import java.util.HashMap;
public class NavDrawerActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, INavActivity, INavActivity2 {
public static final String CURRENT_FRAGMENT1 = "CURRENT_FRAGMENT";
public static final String CURRENT_LOADNUMBER1 = "CURRENT_LOADNUMBER";
Context context;
SessionManager session;
public static String CURRENT_FRAGMENT;
public static int CURRENT_LOADNUMBER;
private static final String TAG = "NavDrawerActivity";
private static final int MY_PHONE_REQUEST_CODE = 140;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nav_drawer);
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setTitle(R.string.app_name_short);
setSupportActionBar(toolbar);
context = this;
session = new SessionManager(context);
Log.i(TAG, "onCreate: ");
DrawerLayout drawer = 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 = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
MainActivityFragment fragment = new MainActivityFragment();
FragmentTransaction fragmentTrans = getSupportFragmentManager().beginTransaction();
fragmentTrans.replace(R.id.fragment_container, fragment);
fragmentTrans.commit();
}
@Override
public void onBackPressed() {
int backStackEntryCount = getSupportFragmentManager().getBackStackEntryCount();
DrawerLayout drawer = findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
}
if (backStackEntryCount == 0) {
android.util.Log.d("TAG", "onBackPressed: a");
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_loads_actions, menu);
return super.onCreateOptionsMenu(menu);
}
public void CallPhone(String PhoneNumber) {
int checkPermission = ContextCompat.checkSelfPermission(context, android.Manifest.permission.CALL_PHONE);
if (checkPermission != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
NavDrawerActivity.this,
new String[]{Manifest.permission.CALL_PHONE},
MY_PHONE_REQUEST_CODE);
} else {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + PhoneNumber));
startActivity(callIntent);
}
}
/**
* On selecting action bar icons
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.action_call_dispatcher:
// call current dispatcher action
HashMap<String, String> user = session.getUserDetails();
final String dispphone = user.get(SessionManager.KEY_DISP_PHONE);
CallPhone(dispphone);
return true;
case R.id.action_call_schugel:
// call main Schugel Number
CallPhone("8003592900");
return true;
case R.id.action_logout:
// refresh
session.logoutUser();
return true;
case R.id.action_settings:
// settings action
Intent intent = new Intent(context, Preferences.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == MY_PHONE_REQUEST_CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(context, "phone call permission granted", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "phone call permission denied", Toast.LENGTH_LONG).show();
}
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.d(TAG, "onSaveInstanceState: ");
outState.putString(CURRENT_FRAGMENT1, CURRENT_FRAGMENT);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.d(TAG, "onRestoreInstanceState: ");
if (savedInstanceState.getString(CURRENT_FRAGMENT1) != null) {
Fragment fragment;
FragmentTransaction fragmentTrans;
switch (savedInstanceState.getString(CURRENT_FRAGMENT1)) {
case "Main":
fragment = new MainActivityFragment();
fragmentTrans = getSupportFragmentManager().beginTransaction();
fragmentTrans.replace(R.id.fragment_container, fragment);
CURRENT_FRAGMENT = "Main";
fragmentTrans.commit();
break;
case "Loads":
setTitle("Load Manager");
fragment = new Loads_Fragment();
fragmentTrans = getSupportFragmentManager().beginTransaction();
fragmentTrans.replace(R.id.fragment_container, fragment);
CURRENT_FRAGMENT = "Loads";
fragmentTrans.commit();
if (LoadsListAdapter.mCurrentLoadNumber != null && getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
Loads_Details_Fragment details = Loads_Details_Fragment.newInstance(LoadsListAdapter.mCurrentLoadNumber);
fragmentTrans = getSupportFragmentManager().beginTransaction();
// Replace any other Fragment with our new Details Fragment with the right data
fragmentTrans.replace(R.id.details, details)
.addToBackStack(null)
// TRANSIT_FRAGMENT_FADE calls for the Fragment to fade away
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.commit();
}
break;
case "Fuelstops":
setTitle("Fuel Stops");
fragment = new MapFragment();
fragmentTrans = getSupportFragmentManager().beginTransaction();
fragmentTrans.replace(R.id.fragment_container, fragment);
CURRENT_FRAGMENT = "Fuelstops";
fragmentTrans.commit();
break;
case "Settlements":
setTitle("Settlements");
fragment = new SettlementFragment();
fragmentTrans = getSupportFragmentManager().beginTransaction();
fragmentTrans.replace(R.id.fragment_container, fragment);
CURRENT_FRAGMENT = "Settlements";
fragmentTrans.commit();
break;
case "Newsletter":
setTitle("Newsletter");
fragment = new NewsletterFragment();
fragmentTrans = getSupportFragmentManager().beginTransaction();
fragmentTrans.replace(R.id.fragment_container, fragment);
CURRENT_FRAGMENT = "Newsletter";
fragmentTrans.commit();
break;
case "Alarmclock":
setTitle("Alarm Clock");
fragment = new AlarmActivity();
fragmentTrans = getSupportFragmentManager().beginTransaction();
fragmentTrans.replace(R.id.fragment_container, fragment);
CURRENT_FRAGMENT = "Alarmclock";
fragmentTrans.commit();
break;
case "Referral":
setTitle("Driver Referral");
fragment = new ReferralFragment();
fragmentTrans = getSupportFragmentManager().beginTransaction();
fragmentTrans.replace(R.id.fragment_container, fragment);
CURRENT_FRAGMENT = "Referral";
fragmentTrans.commit();
break;
case "Accident":
setTitle("Accident Report");
fragment = new AccidentFragment();
fragmentTrans = getSupportFragmentManager().beginTransaction();
fragmentTrans.replace(R.id.fragment_container, fragment);
CURRENT_FRAGMENT = "Accident";
fragmentTrans.commit();
break;
case "Enterload":
setTitle("Enter New Load");
fragment = new Loads_Entry_Fragment();
fragmentTrans = getSupportFragmentManager().beginTransaction();
fragmentTrans.replace(R.id.fragment_container, fragment);
CURRENT_FRAGMENT = "Enterload";
fragmentTrans.commit();
break;
}
}
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_main) {
setTitle("J & R Schugel");
MainActivityFragment fragment = new MainActivityFragment();
FragmentTransaction fragmentTrans = getSupportFragmentManager().beginTransaction();
fragmentTrans.replace(R.id.fragment_container, fragment);
CURRENT_FRAGMENT = "main";
fragmentTrans.commit();
} else if (id == R.id.nav_loads) {
setTitle("Load Manager");
Loads_Fragment fragment = new Loads_Fragment();
FragmentTransaction fragmentTrans = getSupportFragmentManager().beginTransaction();
fragmentTrans.replace(R.id.fragment_container, fragment);
CURRENT_FRAGMENT = "Loads";
fragmentTrans.commit();
} else if (id == R.id.nav_fuelstops) {
setTitle("Fuel Stops");
MapFragment fragment = new MapFragment();
FragmentTransaction fragmentTrans = getSupportFragmentManager().beginTransaction();
fragmentTrans.replace(R.id.fragment_container, fragment);
CURRENT_FRAGMENT = "Fuelstops";
fragmentTrans.commit();
} else if (id == R.id.nav_settlements) {
setTitle("Settlements");
SettlementFragment fragment = new SettlementFragment();
FragmentTransaction fragmentTrans = getSupportFragmentManager().beginTransaction();
fragmentTrans.replace(R.id.fragment_container, fragment);
CURRENT_FRAGMENT = "Settlements";
fragmentTrans.commit();
} else if (id == R.id.nav_newsletter) {
NewsletterFragment fragment = new NewsletterFragment();
FragmentTransaction fragmentTrans = getSupportFragmentManager().beginTransaction();
fragmentTrans.replace(R.id.fragment_container, fragment);
CURRENT_FRAGMENT = "Newsletter";
fragmentTrans.commit();
} else if (id == R.id.nav_alarm_clock) {
AlarmActivity fragment = new AlarmActivity();
FragmentTransaction fragmentTrans = getSupportFragmentManager().beginTransaction();
fragmentTrans.replace(R.id.fragment_container, fragment);
CURRENT_FRAGMENT = "Alarmclock";
fragmentTrans.commit();
} else if (id == R.id.nav_referral) {
ReferralFragment fragment = new ReferralFragment();
FragmentTransaction fragmentTrans = getSupportFragmentManager().beginTransaction();
fragmentTrans.replace(R.id.fragment_container, fragment);
CURRENT_FRAGMENT = "Referral";
fragmentTrans.commit();
} else if (id == R.id.nav_accident) {
AccidentFragment fragment = new AccidentFragment();
FragmentTransaction fragmentTrans = getSupportFragmentManager().beginTransaction();
fragmentTrans.replace(R.id.fragment_container, fragment);
CURRENT_FRAGMENT = "Accident";
fragmentTrans.commit();
} else if (id == R.id.nav_loads_enter) {
Loads_Entry_Fragment fragment = new Loads_Entry_Fragment();
FragmentTransaction fragmentTrans = getSupportFragmentManager().beginTransaction();
fragmentTrans.replace(R.id.fragment_container, fragment);
CURRENT_FRAGMENT = "Enterload";
fragmentTrans.commit();
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private void openLoadDetailsFragment (Fragment fragment, String LoadNumber) {
Bundle bundle = new Bundle();
bundle.putString("frag_load_number", LoadNumber);
fragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction()
.addToBackStack(null)
.replace(R.id.fragment_container, fragment)
.commit();
}
private void openFragment (Fragment fragment, Bundle bundle, String fragmentTag) {
fragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction()
.addToBackStack(null)
.replace(R.id.fragment_container, fragment, fragmentTag)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.commit();
}
@Override
public void inflateFragment(String fragmentTag, String LoadNumber) {
Loads_Details_Fragment fragment = new Loads_Details_Fragment();
openLoadDetailsFragment(fragment, LoadNumber);
}
@Override
public void inflateFragment(Fragment fragment, String fragmentTag, Bundle bundle) {
openFragment(fragment, bundle, fragmentTag);
}
@Override
public void sendEmail(Intent intent) {
this.startActivity(intent);
}
}