Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.33 KB | None | 0 0
  1. package com.example.developerpc.gpstracker;
  2.  
  3. /**
  4. * Created by DeveloperPC on 2.4.2016.
  5. */
  6. import java.io.BufferedReader;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.InputStreamReader;
  10. import java.util.Random;
  11.  
  12. import org.apache.http.HttpEntity;
  13. import org.apache.http.HttpResponse;
  14. import org.apache.http.client.ClientProtocolException;
  15. import org.apache.http.client.methods.HttpPost;
  16. import org.apache.http.client.params.ClientPNames;
  17. import org.apache.http.client.params.CookiePolicy;
  18. import org.apache.http.impl.client.DefaultHttpClient;
  19. import org.apache.http.protocol.BasicHttpContext;
  20. import org.apache.http.protocol.HttpContext;
  21.  
  22. import android.Manifest;
  23. import android.app.Activity;
  24. import android.content.Context;
  25. import android.content.Intent;
  26. import android.content.SharedPreferences;
  27. import android.content.pm.PackageManager;
  28. import android.location.Location;
  29. import android.location.LocationManager;
  30. import android.net.Uri;
  31. import android.os.Bundle;
  32. import android.support.v4.app.ActivityCompat;
  33. import android.telephony.TelephonyManager;
  34. import android.util.Log;
  35. import android.view.View;
  36. import android.view.View.OnClickListener;
  37. import android.widget.ImageButton;
  38. import android.widget.TextView;
  39. import android.widget.Toast;
  40.  
  41. import com.google.android.gms.appindexing.Action;
  42. import com.google.android.gms.appindexing.AppIndex;
  43. import com.google.android.gms.common.api.GoogleApiClient;
  44.  
  45. public class HomeScreen extends Activity {
  46.  
  47. static final String AB = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  48. static Random rnd = new Random();
  49.  
  50. private static LocationManager mLocationmanager=null;
  51. Double _curLat;
  52. Double _curLng;
  53. TextView _uidText, _passText, _latText, _lngText;
  54. /**
  55. * ATTENTION: This was auto-generated to implement the App Indexing API.
  56. * See https://g.co/AppIndexing/AndroidStudio for more information.
  57. */
  58. private GoogleApiClient client;
  59.  
  60. @Override
  61. public void onCreate(Bundle savedInstanceState) {
  62. super.onCreate(savedInstanceState);
  63. setContentView(R.layout.main);
  64.  
  65. SetGPSLocation();
  66. TelephonyManager TelephonyMgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
  67. String deviceIMEI = TelephonyMgr.getDeviceId();
  68.  
  69.  
  70. SharedPreferences preferences = getPreferences(MODE_WORLD_WRITEABLE
  71. | MODE_WORLD_READABLE);
  72. SharedPreferences.Editor editor = preferences.edit();
  73.  
  74. String _storedPass = preferences.getString("_pass", null);
  75.  
  76. _uidText = (TextView) findViewById(R.id.TextView02);
  77. _passText = (TextView) findViewById(R.id.TextView04);
  78. _latText = (TextView) findViewById(R.id.TextView06);
  79. _lngText = (TextView) findViewById(R.id.TextView08);
  80.  
  81. _uidText.setText(deviceIMEI);
  82. _latText.setText(Double.toString(_curLat));
  83. _lngText.setText(Double.toString(_curLng));
  84.  
  85. if (_storedPass == null) {
  86. String temp = randomString(10);
  87. _passText.setText(temp);
  88. editor.putString("_pass", temp);
  89. addUser(deviceIMEI, temp);
  90. } else {
  91. _passText.setText(_storedPass);
  92. editor.putString("_pass", _storedPass);
  93. }
  94.  
  95. editor.commit();
  96.  
  97. ImageButton _settings = (ImageButton) findViewById(R.id.ImageButton01);
  98. _settings.setOnClickListener(new OnClickListener() {
  99. @Override
  100. public void onClick(View v) {
  101. Intent myIntent = new Intent();
  102. myIntent.setClassName("com.example.developerpc.gpstracker", "com.example.developerpc.gpstracker.Settings")
  103. .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  104. startActivity(myIntent);
  105. }
  106. });
  107. // ATTENTION: This was auto-generated to implement the App Indexing API.
  108. // See https://g.co/AppIndexing/AndroidStudio for more information.
  109. client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
  110. }
  111.  
  112. String randomString(int len) {
  113. StringBuilder sb = new StringBuilder(len);
  114. for (int i = 0; i < len; i++)
  115. sb.append(AB.charAt(rnd.nextInt(AB.length())));
  116. return sb.toString();
  117. }
  118.  
  119. public void SetGPSLocation() {
  120. if ((ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) && (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
  121. // TODO: Consider calling
  122. // ActivityCompat#requestPermissions
  123. // here to request the missing permissions, and then overriding
  124. // public void onRequestPermissionsResult(int requestCode, String[] permissions,
  125. // int[] grantResults)
  126. // to handle the case where the user grants the permission. See the documentation
  127. // for ActivityCompat#requestPermissions for more details.
  128. return;
  129. }
  130. if ((ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) && (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
  131. // TODO: Consider calling
  132. // ActivityCompat#requestPermissions
  133. // here to request the missing permissions, and then overriding
  134. // public void onRequestPermissionsResult(int requestCode, String[] permissions,
  135. // int[] grantResults)
  136. // to handle the case where the user grants the permission. See the documentation
  137. // for ActivityCompat#requestPermissions for more details.
  138. return;
  139. }
  140. mLocationmanager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
  141. Location l = mLocationmanager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  142.  
  143. if (l != null) {
  144. _curLat = l.getLatitude();
  145. _curLng = l.getLongitude();
  146. } else {
  147. Location l1 = mLocationmanager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  148.  
  149. if (l1 != null) {
  150. _curLat = l1.getLatitude();
  151. _curLng = l1.getLongitude();
  152. } else {
  153. _curLat = 0.0;
  154. _curLng = 0.0;
  155. Toast.makeText(HomeScreen.this, "NO GPS location found",
  156. Toast.LENGTH_SHORT).show();
  157. }
  158.  
  159. }
  160.  
  161.  
  162.  
  163. }
  164.  
  165.  
  166. public String addUser(String devid, String password) {
  167. DefaultHttpClient httpClient = new DefaultHttpClient();
  168. HttpContext localContext = new BasicHttpContext();
  169.  
  170. HttpResponse response = null;
  171. HttpPost httpPost = null;
  172.  
  173. String url = "http://192.168.1.220/androspy/addUser.php?DeviceID="
  174. + devid
  175. + "&password=" + password;
  176.  
  177. Log.d("url", url);
  178.  
  179. httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY,CookiePolicy.RFC_2109);
  180.  
  181. httpPost = new HttpPost(url);
  182. response = null;
  183.  
  184. try {
  185. response = httpClient.execute(httpPost, localContext);
  186.  
  187. } catch (ClientProtocolException e) {
  188. System.out.println("HTTPHelp : ClientProtocolException : " + e);
  189. } catch (IOException e) {
  190. System.out.println("HTTPHelp : IOException : " + e);
  191. }
  192.  
  193. HttpEntity entity = response.getEntity();
  194. String result = "error";
  195.  
  196. InputStream instream;
  197. try {
  198. instream = entity.getContent();
  199.  
  200. result = convertStreamToString(instream);
  201. } catch (IllegalStateException e) {
  202. e.printStackTrace();
  203. } catch (IOException e) {
  204. // TODO Auto-generated catch block
  205. e.printStackTrace();
  206. }
  207. return result;
  208. }
  209.  
  210. private static String convertStreamToString(InputStream is) {
  211. BufferedReader reader = new BufferedReader(new InputStreamReader(is));
  212. StringBuilder sb = new StringBuilder();
  213.  
  214. String line = null;
  215. try {
  216. while ((line = reader.readLine()) != null) {
  217. sb.append(line + "n");
  218. }
  219. } catch (IOException e) {
  220. e.printStackTrace();
  221. } finally {
  222. try {
  223. is.close();
  224. } catch (IOException e) {
  225. e.printStackTrace();
  226. }
  227. }
  228. return sb.toString();
  229. }
  230.  
  231. @Override
  232. public void onStart() {
  233. super.onStart();
  234.  
  235. // ATTENTION: This was auto-generated to implement the App Indexing API.
  236. // See https://g.co/AppIndexing/AndroidStudio for more information.
  237. client.connect();
  238. Action viewAction = Action.newAction(
  239. Action.TYPE_VIEW, // TODO: choose an action type.
  240. "HomeScreen Page", // TODO: Define a title for the content shown.
  241. // TODO: If you have web page content that matches this app activity's content,
  242. // make sure this auto-generated web page URL is correct.
  243. // Otherwise, set the URL to null.
  244. Uri.parse("http://host/path"),
  245. // TODO: Make sure this auto-generated app deep link URI is correct.
  246. Uri.parse("android-app://com.example.developerpc.gpstracker/http/host/path")
  247. );
  248. AppIndex.AppIndexApi.start(client, viewAction);
  249. }
  250.  
  251. @Override
  252. public void onStop() {
  253. super.onStop();
  254.  
  255. // ATTENTION: This was auto-generated to implement the App Indexing API.
  256. // See https://g.co/AppIndexing/AndroidStudio for more information.
  257. Action viewAction = Action.newAction(
  258. Action.TYPE_VIEW, // TODO: choose an action type.
  259. "HomeScreen Page", // TODO: Define a title for the content shown.
  260. // TODO: If you have web page content that matches this app activity's content,
  261. // make sure this auto-generated web page URL is correct.
  262. // Otherwise, set the URL to null.
  263. Uri.parse("http://host/path"),
  264. // TODO: Make sure this auto-generated app deep link URI is correct.
  265. Uri.parse("android-app://com.example.developerpc.gpstracker/http/host/path")
  266. );
  267. AppIndex.AppIndexApi.end(client, viewAction);
  268. client.disconnect();
  269. }
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement