Advertisement
Guest User

Untitled

a guest
Jan 29th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.17 KB | None | 0 0
  1. package com.example.qistina.myguardian;
  2.  
  3.  
  4. import android.Manifest;
  5. import android.app.Activity;
  6. import android.app.AlertDialog;
  7. import android.app.Dialog;
  8. import android.content.Context;
  9. import android.content.DialogInterface;
  10. import android.content.Intent;
  11. import android.content.SharedPreferences;
  12. import android.content.pm.PackageManager;
  13. import android.location.Address;
  14. import android.location.Geocoder;
  15. import android.location.Location;
  16. import android.location.LocationListener;
  17. import android.location.LocationManager;
  18. import android.media.AudioManager;
  19. import android.media.MediaPlayer;
  20. import android.net.Uri;
  21. import android.os.AsyncTask;
  22. import android.os.Build;
  23. import android.os.Bundle;
  24. import android.app.Fragment;
  25. import android.support.annotation.NonNull;
  26. import android.support.annotation.Nullable;
  27. import android.support.v4.app.ActivityCompat;
  28. import android.support.v4.content.ContextCompat;
  29. import android.telephony.SmsManager;
  30. import android.view.LayoutInflater;
  31. import android.view.View;
  32. import android.view.ViewGroup;
  33. import android.widget.Button;
  34. import android.widget.CompoundButton;
  35. import android.widget.ImageButton;
  36. import android.widget.ImageView;
  37. import android.widget.Switch;
  38. import android.widget.Toast;
  39. import android.widget.ToggleButton;
  40.  
  41. import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
  42. import com.google.android.gms.common.api.GoogleApiClient;
  43. import com.google.android.gms.maps.CameraUpdate;
  44. import com.google.android.gms.maps.CameraUpdateFactory;
  45. import com.google.android.gms.maps.GoogleMap;
  46. import com.google.android.gms.maps.MapFragment;
  47. import com.google.android.gms.maps.MapView;
  48. import com.google.android.gms.maps.MapsInitializer;
  49. import com.google.android.gms.maps.OnMapReadyCallback;
  50. import com.google.android.gms.maps.SupportMapFragment;
  51. import com.google.android.gms.maps.model.LatLng;
  52. import com.google.android.gms.maps.model.MarkerOptions;
  53. import com.karan.churi.PermissionManager.PermissionManager;
  54.  
  55. import java.io.IOException;
  56. import java.sql.Connection;
  57. import java.sql.DriverManager;
  58. import java.sql.ResultSet;
  59. import java.sql.Statement;
  60. import java.util.List;
  61.  
  62. import static android.content.Context.AUDIO_SERVICE;
  63.  
  64.  
  65. /**
  66. * A simple {@link Fragment} subclass.
  67. */
  68. public class HomeFragment extends Fragment implements OnMapReadyCallback {
  69.  
  70. ImageButton callBtn, msgBtn;
  71. Switch alarmBtn;
  72. MapView mapView;
  73. GoogleMap map;
  74. PermissionManager permissionManager;
  75. LocationManager locationManager;
  76. SharedPreferences sharedpreferences;
  77. String contact1, contact2, contact3, contact4;
  78.  
  79. public HomeFragment() {
  80. // Required empty public constructor
  81. }
  82.  
  83.  
  84. @Override
  85. public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
  86. @Nullable Bundle savedInstanceState) {
  87.  
  88. // Inflate the layout for this fragment
  89. View v = inflater.inflate(R.layout.fragment_home, container, false);
  90. mapView = v.findViewById(R.id.mapView);
  91. mapView.onCreate(savedInstanceState);
  92. mapView.getMapAsync(this);
  93. permissionManager = new PermissionManager() {
  94. };
  95. permissionManager.checkAndRequestPermissions(getActivity());
  96.  
  97.  
  98. AudioManager audioManager = (AudioManager) getActivity().getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
  99. int maxVolumeMusic = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
  100. audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolumeMusic, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
  101.  
  102.  
  103. final MediaPlayer alarm = MediaPlayer.create(getActivity().getApplicationContext(), R.raw.alarm);
  104. alarm.setAudioStreamType(AudioManager.STREAM_MUSIC);
  105.  
  106. GetContactList getContactList = new GetContactList();
  107. getContactList.execute();
  108.  
  109. callBtn = (ImageButton) v.findViewById(R.id.abcall);
  110. msgBtn = (ImageButton) v.findViewById(R.id.abnotify);
  111. alarmBtn = (Switch) v.findViewById(R.id.alarmSwitch);
  112.  
  113.  
  114. callBtn.setOnClickListener(new View.OnClickListener() { //INSTEAD YOU EMBED A LISTENER TO THAT BUTTON
  115. @Override
  116. public void onClick(View v) {
  117.  
  118. confirmCallDialog();
  119.  
  120. }
  121.  
  122.  
  123. });
  124.  
  125.  
  126. msgBtn.setOnClickListener(new View.OnClickListener() { //INSTEAD YOU EMBED A LISTENER TO THAT BUTTON
  127. @Override
  128. public void onClick(View v) {
  129.  
  130. confirmNotifyDialog();
  131.  
  132. }
  133.  
  134. });
  135.  
  136. alarmBtn.setOnCheckedChangeListener(
  137. new CompoundButton.OnCheckedChangeListener() {
  138. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  139. if (isChecked) {
  140.  
  141.  
  142. alarm.start();
  143. System.out.println("heh");
  144.  
  145.  
  146. } else {
  147.  
  148. alarm.stop();
  149. alarm.prepareAsync();
  150.  
  151. }
  152. }
  153. });
  154.  
  155.  
  156. return v;
  157.  
  158. }
  159.  
  160.  
  161.  
  162. @Override
  163. public void onMapReady(GoogleMap googleMap) {
  164. map = googleMap;
  165. locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
  166. System.out.println("test2323");
  167. if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
  168. // permissionManager.checkAndRequestPermissions(getActivity());
  169. map.setMyLocationEnabled(true);
  170. mapView.onStart();
  171. System.out.println("workssss");
  172.  
  173.  
  174. }
  175. }
  176.  
  177.  
  178. @Override
  179. public void onResume() {
  180. mapView.onResume();
  181. System.out.println("wthi");
  182. super.onResume();
  183. }
  184.  
  185. @Override
  186. public void onDestroy() {
  187. super.onDestroy();
  188. System.out.println("huhuhuuhuh");
  189. mapView.onDestroy();
  190. }
  191.  
  192. @Override
  193. public void onLowMemory() {
  194. super.onLowMemory();
  195. mapView.onLowMemory();
  196. System.out.println("erm");
  197. }
  198. //confirm dialogs
  199.  
  200. //call confirm
  201.  
  202. private void confirmCallDialog(){
  203.  
  204. AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  205. builder.setCancelable(true);
  206. builder.setTitle("Call Emergency Services");
  207. builder.setMessage("Are you sure you want to make a call to Emergency Services?");
  208. builder.setPositiveButton("Confirm",
  209. new DialogInterface.OnClickListener() {
  210. @Override
  211. public void onClick(DialogInterface dialog, int which) {
  212. //call
  213.  
  214. String number = "0193564514";
  215. Intent intent = new Intent(Intent.ACTION_CALL);
  216. intent.setData(Uri.parse("tel:" + number));
  217. startActivity(intent);
  218.  
  219.  
  220. }
  221. });
  222.  
  223. builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  224. @Override
  225. public void onClick(DialogInterface dialog, int which) {
  226. }
  227. });
  228.  
  229. AlertDialog dialog = builder.create();
  230. dialog.show();
  231. }
  232.  
  233.  
  234. private void confirmNotifyDialog(){
  235. AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  236. builder.setCancelable(true);
  237. builder.setTitle("");
  238. builder.setMessage("Send an emergency text message to contacts?");
  239. builder.setPositiveButton("Confirm",
  240. new DialogInterface.OnClickListener() {
  241. @Override
  242. public void onClick(DialogInterface dialog, int which) {
  243. //send msg
  244.  
  245. String messageToSend = "please help";
  246. String number = contact1;
  247. String number2 = contact2;
  248. String number3 = contact3;
  249. String number4 = contact4;
  250.  
  251. if (!number.equals("")){
  252. SmsManager.getDefault().sendTextMessage(number, null, messageToSend, null,null);
  253. }
  254. if (!number2.equals("")) {
  255. SmsManager.getDefault().sendTextMessage(number2, null, messageToSend, null,null);
  256. }
  257. if (!number3.equals("")) {
  258. SmsManager.getDefault().sendTextMessage(number3, null, messageToSend, null,null);
  259. }
  260. if (!number4.equals("")) {
  261. SmsManager.getDefault().sendTextMessage(number4, null, messageToSend, null,null);
  262. }
  263. if (number.equals("") && number2.equals("") && number3.equals("") && number4.equals("")) {
  264. Toast.makeText(getActivity().getApplicationContext(), "Oops! Please add contacts to be able to send an emergency text.", Toast.LENGTH_SHORT).show();
  265. }
  266.  
  267. }
  268. });
  269. builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  270. @Override
  271. public void onClick(DialogInterface dialog, int which) {
  272. }
  273. });
  274.  
  275. AlertDialog dialog = builder.create();
  276. dialog.show();
  277. }
  278.  
  279. @Override
  280. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  281. permissionManager.checkResult(requestCode,permissions,grantResults);
  282.  
  283. }
  284.  
  285.  
  286. private class GetContactList extends AsyncTask<String, String, Integer> {
  287.  
  288. @Override
  289. protected Integer doInBackground(String... contactList) {
  290.  
  291. sharedpreferences = getActivity().getSharedPreferences(LoginFragment.MyPREFERENCES, Context.MODE_PRIVATE);
  292. String email = sharedpreferences.getString(LoginFragment.Email, "");
  293.  
  294. int y = 0;
  295. try {
  296.  
  297. Class.forName("com.mysql.jdbc.Driver");
  298. Connection con = DriverManager.getConnection("jdbc:mysql://" + MainActivity.ip + ":3306/myguardian", "myguardian", "fyp123");
  299. Statement stmt = con.createStatement();
  300. ResultSet rs = stmt.executeQuery("select * from contacts where email = '" + email + "';");
  301. while (rs.next()) {
  302.  
  303. contact1 = rs.getString(4);
  304. contact2 = rs.getString(6);
  305. contact3 = rs.getString(8);
  306. contact4 = rs.getString(10);
  307.  
  308. System.out.println(contact1);
  309. System.out.println(contact2);
  310. System.out.println(contact3);
  311. System.out.println(contact4);
  312.  
  313.  
  314. }
  315.  
  316. } catch (Exception e) {
  317. System.out.println(e);
  318. }
  319.  
  320. return y;
  321. }
  322. }
  323.  
  324.  
  325.  
  326. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement