-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9.1P.java
More file actions
592 lines (476 loc) · 21.5 KB
/
9.1P.java
File metadata and controls
592 lines (476 loc) · 21.5 KB
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
// Data
// DatabaseHelper.java
package com.example.trucksharingapp.data;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
import com.example.trucksharingapp.model.User;
import com.example.trucksharingapp.util.Util;
import java.util.ArrayList;
import java.util.List;
public class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(@Nullable Context context) {
super(context, Util.DATABASE_NAME, null, Util.DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String CREATE_USER_TABLE = "CREATE TABLE " +
Util.TABLE_NAME + "(" +
Util.COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
Util.COLUMN_POST_TYPE + " TEXT, " +
Util.COLUMN_NAME + " TEXT, " +
Util.COLUMN_PHONE + " TEXT," +
Util.COLUMN_DESCRIPTION + " TEXT," +
Util.COLUMN_DATE + " TEXT," +
Util.COLUMN_LOCATION + " TEXT" + ")";
sqLiteDatabase.execSQL(CREATE_USER_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + Util.TABLE_NAME);
onCreate(sqLiteDatabase);
}
public long insertUser(User user) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(Util.COLUMN_POST_TYPE, user.post_type);
contentValues.put(Util.COLUMN_NAME, user.name);
contentValues.put(Util.COLUMN_PHONE, user.phone);
contentValues.put(Util.COLUMN_DESCRIPTION, user.description);
contentValues.put(Util.COLUMN_DATE, user.date);
contentValues.put(Util.COLUMN_LOCATION, user.location);
long newRowId = db.insert(Util.TABLE_NAME, null, contentValues);
db.close();
return newRowId;
}
public User fetchUser(int id) {
SQLiteDatabase db = this.getReadableDatabase();
@SuppressLint("Recycle") Cursor cursor = db.rawQuery("SELECT * FROM " + Util.TABLE_NAME + " WHERE " + Util.COLUMN_ID + " = ?", new String[]{String.valueOf(id)});
if (cursor != null)
cursor.moveToFirst();
assert cursor != null;
return new User(cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6));
}
public List<User> fetchAllUsers() {
List<User> userList = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
String selectAll = " SELECT * FROM " + Util.TABLE_NAME;
Cursor cursor = db.rawQuery(selectAll, null);
if (cursor.moveToFirst()) {
do {
User user = new User(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6));
userList.add(user);
} while (cursor.moveToNext());
}
return userList;
}
public void deleteItems(int id) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(Util.TABLE_NAME, Util.COLUMN_ID + "=?", new String[]{String.valueOf(id)});
}
}
// ItemDetails.java
package com.example.trucksharingapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import com.example.trucksharingapp.data.DatabaseHelper;
import com.example.trucksharingapp.model.User;
public class ItemDetails extends AppCompatActivity {
DatabaseHelper db;
public TextView postNameTxt, descTxt, dateTxt, locationTxt, phoneTxt;
public Button removeBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_details);
postNameTxt = findViewById(R.id.post_name_txt);
descTxt = findViewById(R.id.description_txt);
dateTxt = findViewById(R.id.date_txt);
locationTxt = findViewById(R.id.location_txt);
phoneTxt = findViewById(R.id.phone_txt);
removeBtn = findViewById(R.id.removeItem);
db = new DatabaseHelper(this);
Intent intent = getIntent();
int id = intent.getIntExtra("id", 0);
User user = db.fetchUser(id);
postNameTxt.setText(user.post_type + " " + user.description);
descTxt.setText("Name : " + user.name);
dateTxt.setText("Date : " + user.date);
locationTxt.setText("Location : " + user.location);
phoneTxt.setText("Phone Number : " + user.phone);
// to remove notes
removeBtn.setOnClickListener(view -> {
db.deleteItems(id);
setResult(RESULT_OK);
finish();
});
}
}
// ListViewAdapter.java
package com.example.trucksharingapp;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import com.example.trucksharingapp.model.User;
import java.util.List;
public class ListViewAdapter extends ArrayAdapter<User> {
public List<User> users;
public int resource;
public OnClicked onClicked;
public ListViewAdapter(@NonNull Context context,int resource,List<User> users, OnClicked onClicked) {
super(context,R.layout.item_list,resource,users);
this.users = users;
this.resource = resource;
this.onClicked = onClicked;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view;
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resource, null);
} else {
view = convertView;
}
TextView post_type = view.findViewById(R.id.post_type);
TextView description = view.findViewById(R.id.description);
ConstraintLayout listLayout = view.findViewById(R.id.list_layout);
User user = users.get(position);
post_type.setText(user.post_type);
description.setText(user.description);
listLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onClicked.onClicked(user);
}
});
return view;
}
}
// MainActivity.java
package com.example.trucksharingapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button showlfButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button signupButton = findViewById(R.id.signUpButton);
Button showButton = findViewById(R.id.showButton);
showlfButton = findViewById(R.id.showlfButton);
signupButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent signupIntent = new Intent(MainActivity.this, SignupActivity.class);
startActivity(signupIntent);
}
});
showButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent showIntent = new Intent(MainActivity.this, ShowUsers.class);
startActivity(showIntent);
}
});
showlfButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, MapsActivity.class);
startActivity(intent);
}
});
}
}
// MapsActivity.java
package com.example.trucksharingapp;
import androidx.fragment.app.FragmentActivity;
import android.os.Bundle;
import com.example.trucksharingapp.data.DatabaseHelper;
import com.example.trucksharingapp.model.User;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.example.trucksharingapp.databinding.ActivityMapsBinding;
import java.util.List;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private ActivityMapsBinding binding;
public double latitude, longitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMapsBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
DatabaseHelper databaseHelper = new DatabaseHelper(this);
List<User> locationList = databaseHelper.fetchAllUsers();
for (User userLocation : locationList) {
// https://stackoverflow.com/questions/20249274/find-and-replace-android-studio
String lat_lng = String.valueOf(userLocation.location);
lat_lng = lat_lng.replaceAll("lat/lng: ", "");
lat_lng = lat_lng.replace("(", "");
lat_lng = lat_lng.replace(")", "");
String[] split = lat_lng.split(",");
latitude = Double.parseDouble(split[0]);
longitude = Double.parseDouble(split[1]);
googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title(userLocation.description));
}
}
}
// Model
// User.java
package com.example.trucksharingapp.model;
public class User {
public int id;
public String post_type;
public String name;
public String phone;
public String description;
public String date;
public String location;
public User(int id, String post_type ,String name, String phone, String description, String date, String location) {
this.id = id;
this.post_type = post_type;
this.name = name;
this.phone = phone;
this.description = description;
this.date = date;
this.location = location;
}
public User(String post_type ,String name, String phone, String description, String date, String location) {
this.post_type = post_type;
this.name = name;
this.phone = phone;
this.description = description;
this.date = date;
this.location = location;
}
}
// ShowUsers.java
package com.example.trucksharingapp;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;
import com.example.trucksharingapp.data.DatabaseHelper;
import com.example.trucksharingapp.model.User;
import java.util.List;
public class ShowUsers extends AppCompatActivity implements OnClicked {
ListView usersListView ;
ListViewAdapter adapter;
ActivityResultLauncher<Intent> startActivityForResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_users);
usersListView = findViewById(R.id.usersListView);
DatabaseHelper db = new DatabaseHelper(ShowUsers.this);
startActivityForResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
List<User> userList = db.fetchAllUsers();
adapter = new ListViewAdapter(this, R.layout.item_list, userList, this);
usersListView.setAdapter(adapter);
});
List<User> userList = db.fetchAllUsers();
adapter = new ListViewAdapter(this, R.layout.item_list, userList, this);
usersListView.setAdapter(adapter);
}
@Override
public void onClicked(User user) {
Intent intent = new Intent(this, ItemDetails.class);
intent.putExtra("id", user.id);
startActivityForResult.launch(intent);
}
}
// SignupActivity.java
{
public EditText name, phone ,description, date;
public Button save;
public RadioButton lost, found;
public RadioGroup radioGroup;
EditText locationedit;
LocationManager locationManager;
Button currentLoc;
LocationListener locationListener;
final Calendar myCalendar = Calendar.getInstance();
public static String post_type = "", show_loc = "";
DatabaseHelper db;
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED ) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
name = findViewById(R.id.name_edt);
phone = findViewById(R.id.phone_edt);
description = findViewById(R.id.desc_edt);
save = findViewById(R.id.save_btn);
lost = findViewById(R.id.lostRB);
found = findViewById(R.id.foundRB);
radioGroup = findViewById(R.id.radioGr);
date = findViewById(R.id.date_edt);
locationedit = findViewById(R.id.location_edt);
currentLoc = findViewById(R.id.currentLoc);
db = new DatabaseHelper(this);
// to initialize places and enter API key
Places.initialize(getApplicationContext(), "AIzaSyBAf7rpSKl19V2rMIHMDQM-tUZUm3LSnw0");
// non focusable
locationedit.setFocusable(false);
locationedit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
List<Place.Field> fieldList = Arrays.asList(Place.Field.ADDRESS, Place.Field.LAT_LNG, Place.Field.NAME);
Intent i = new Autocomplete.IntentBuilder(AutocompleteActivityMode.FULLSCREEN, fieldList).build(SignupActivity.this);
startActivityForResult(i, 100);
}
});
DatePickerDialog.OnDateSetListener datePick = (view, year, monthOfYear, dayOfMonth) -> {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String myFormat = "dd/MM/yyyy";
SimpleDateFormat dateFormat = new SimpleDateFormat(myFormat, Locale.US);
date.setText(dateFormat.format(myCalendar.getTime()));
};
// https://stackoverflow.com/questions/14327412/set-focus-on-edittext
date.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus){
new DatePickerDialog(SignupActivity.this, datePick, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH)).show(); }
}
});
radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
switch (checkedId) {
case R.id.lostRB:
post_type = "lost";
break;
case R.id.foundRB:
post_type = "found";
break;
}
});
locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
currentLoc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
locationListener = new LocationListener() {
@Override
public void onLocationChanged(@NonNull Location location) {
locationedit.setText("lat/lng: (" + location.getLatitude() + ", " + location.getLongitude() + ")");
show_loc = locationedit.getText().toString();
}
};
if (ActivityCompat.checkSelfPermission(SignupActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission
(SignupActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
ActivityCompat.requestPermissions(SignupActivity.this, new String[]{ Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
}
else {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
}
});
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name_str = name.getText().toString();
String phone_str = phone.getText().toString();
String location_str = show_loc;
String description_str = description.getText().toString();
String date_str = date.getText().toString();
if (name_str.isEmpty() || phone_str.isEmpty() || location_str.isEmpty() || description_str.isEmpty() || date_str.isEmpty() || post_type.isEmpty()) {
Toast.makeText(SignupActivity.this, "PLEASE FILL-IN THE DATA", Toast.LENGTH_SHORT).show();
} else {
db.insertUser(new User(post_type, name_str, phone_str, description_str, date_str, location_str));
Intent intent = new Intent(SignupActivity.this, MainActivity.class );
startActivity(intent);
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100 && resultCode == RESULT_OK){
Place place = Autocomplete.getPlaceFromIntent(data);
locationedit.setText(place.getAddress());
show_loc = place.getLatLng().toString();
}
else if (resultCode == AutocompleteActivity.RESULT_ERROR){
Status status = Autocomplete.getStatusFromIntent(data);
Toast.makeText(getApplicationContext(),status.getStatusMessage(), Toast.LENGTH_SHORT).show();
}
}
}
// Util.java
package com.example.trucksharingapp.util;
public class Util {
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "user_db";
public static final String TABLE_NAME = "users";
public static final String COLUMN_ID = "id";
public static final String COLUMN_POST_TYPE = "post_type";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_PHONE = "phone";
public static final String COLUMN_DESCRIPTION = "description";
public static final String COLUMN_DATE = "date";
public static final String COLUMN_LOCATION = "location";
}