Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.developerpc.gpstracker;
- /**
- * Created by DeveloperPC on 2.4.2016.
- */
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.util.Random;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.ClientProtocolException;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.client.params.ClientPNames;
- import org.apache.http.client.params.CookiePolicy;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.protocol.BasicHttpContext;
- import org.apache.http.protocol.HttpContext;
- import android.Manifest;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.content.pm.PackageManager;
- import android.location.Location;
- import android.location.LocationManager;
- import android.net.Uri;
- import android.os.Bundle;
- import android.support.v4.app.ActivityCompat;
- import android.telephony.TelephonyManager;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.ImageButton;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.google.android.gms.appindexing.Action;
- import com.google.android.gms.appindexing.AppIndex;
- import com.google.android.gms.common.api.GoogleApiClient;
- public class HomeScreen extends Activity {
- static final String AB = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- static Random rnd = new Random();
- private static LocationManager mLocationmanager=null;
- Double _curLat;
- Double _curLng;
- TextView _uidText, _passText, _latText, _lngText;
- /**
- * ATTENTION: This was auto-generated to implement the App Indexing API.
- * See https://g.co/AppIndexing/AndroidStudio for more information.
- */
- private GoogleApiClient client;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- SetGPSLocation();
- TelephonyManager TelephonyMgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
- String deviceIMEI = TelephonyMgr.getDeviceId();
- SharedPreferences preferences = getPreferences(MODE_WORLD_WRITEABLE
- | MODE_WORLD_READABLE);
- SharedPreferences.Editor editor = preferences.edit();
- String _storedPass = preferences.getString("_pass", null);
- _uidText = (TextView) findViewById(R.id.TextView02);
- _passText = (TextView) findViewById(R.id.TextView04);
- _latText = (TextView) findViewById(R.id.TextView06);
- _lngText = (TextView) findViewById(R.id.TextView08);
- _uidText.setText(deviceIMEI);
- _latText.setText(Double.toString(_curLat));
- _lngText.setText(Double.toString(_curLng));
- if (_storedPass == null) {
- String temp = randomString(10);
- _passText.setText(temp);
- editor.putString("_pass", temp);
- addUser(deviceIMEI, temp);
- } else {
- _passText.setText(_storedPass);
- editor.putString("_pass", _storedPass);
- }
- editor.commit();
- ImageButton _settings = (ImageButton) findViewById(R.id.ImageButton01);
- _settings.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent myIntent = new Intent();
- myIntent.setClassName("com.example.developerpc.gpstracker", "com.example.developerpc.gpstracker.Settings")
- .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- startActivity(myIntent);
- }
- });
- // ATTENTION: This was auto-generated to implement the App Indexing API.
- // See https://g.co/AppIndexing/AndroidStudio for more information.
- client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
- }
- String randomString(int len) {
- StringBuilder sb = new StringBuilder(len);
- for (int i = 0; i < len; i++)
- sb.append(AB.charAt(rnd.nextInt(AB.length())));
- return sb.toString();
- }
- public void SetGPSLocation() {
- if ((ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) && (ActivityCompat.checkSelfPermission(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.
- return;
- }
- if ((ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) && (ActivityCompat.checkSelfPermission(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.
- return;
- }
- mLocationmanager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
- Location l = mLocationmanager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
- if (l != null) {
- _curLat = l.getLatitude();
- _curLng = l.getLongitude();
- } else {
- Location l1 = mLocationmanager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
- if (l1 != null) {
- _curLat = l1.getLatitude();
- _curLng = l1.getLongitude();
- } else {
- _curLat = 0.0;
- _curLng = 0.0;
- Toast.makeText(HomeScreen.this, "NO GPS location found",
- Toast.LENGTH_SHORT).show();
- }
- }
- }
- public String addUser(String devid, String password) {
- DefaultHttpClient httpClient = new DefaultHttpClient();
- HttpContext localContext = new BasicHttpContext();
- HttpResponse response = null;
- HttpPost httpPost = null;
- String url = "http://192.168.1.220/androspy/addUser.php?DeviceID="
- + devid
- + "&password=" + password;
- Log.d("url", url);
- httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY,CookiePolicy.RFC_2109);
- httpPost = new HttpPost(url);
- response = null;
- try {
- response = httpClient.execute(httpPost, localContext);
- } catch (ClientProtocolException e) {
- System.out.println("HTTPHelp : ClientProtocolException : " + e);
- } catch (IOException e) {
- System.out.println("HTTPHelp : IOException : " + e);
- }
- HttpEntity entity = response.getEntity();
- String result = "error";
- InputStream instream;
- try {
- instream = entity.getContent();
- result = convertStreamToString(instream);
- } catch (IllegalStateException e) {
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return result;
- }
- private static String convertStreamToString(InputStream is) {
- BufferedReader reader = new BufferedReader(new InputStreamReader(is));
- StringBuilder sb = new StringBuilder();
- String line = null;
- try {
- while ((line = reader.readLine()) != null) {
- sb.append(line + "n");
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- is.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return sb.toString();
- }
- @Override
- public void onStart() {
- super.onStart();
- // ATTENTION: This was auto-generated to implement the App Indexing API.
- // See https://g.co/AppIndexing/AndroidStudio for more information.
- client.connect();
- Action viewAction = Action.newAction(
- Action.TYPE_VIEW, // TODO: choose an action type.
- "HomeScreen Page", // TODO: Define a title for the content shown.
- // TODO: If you have web page content that matches this app activity's content,
- // make sure this auto-generated web page URL is correct.
- // Otherwise, set the URL to null.
- Uri.parse("http://host/path"),
- // TODO: Make sure this auto-generated app deep link URI is correct.
- Uri.parse("android-app://com.example.developerpc.gpstracker/http/host/path")
- );
- AppIndex.AppIndexApi.start(client, viewAction);
- }
- @Override
- public void onStop() {
- super.onStop();
- // ATTENTION: This was auto-generated to implement the App Indexing API.
- // See https://g.co/AppIndexing/AndroidStudio for more information.
- Action viewAction = Action.newAction(
- Action.TYPE_VIEW, // TODO: choose an action type.
- "HomeScreen Page", // TODO: Define a title for the content shown.
- // TODO: If you have web page content that matches this app activity's content,
- // make sure this auto-generated web page URL is correct.
- // Otherwise, set the URL to null.
- Uri.parse("http://host/path"),
- // TODO: Make sure this auto-generated app deep link URI is correct.
- Uri.parse("android-app://com.example.developerpc.gpstracker/http/host/path")
- );
- AppIndex.AppIndexApi.end(client, viewAction);
- client.disconnect();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement