Advertisement
Guest User

MainActivity.class

a guest
Aug 20th, 2016
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.49 KB | None | 0 0
  1. import android.Manifest;
  2. import android.content.Context;
  3. import android.content.pm.PackageManager;
  4. import android.location.Location;
  5. import android.media.Image;
  6. import android.net.ConnectivityManager;
  7. import android.net.NetworkInfo;
  8. import android.net.Uri;
  9. import android.nfc.Tag;
  10. import android.support.annotation.NonNull;
  11. import android.support.annotation.Nullable;
  12. import android.support.v4.app.ActivityCompat;
  13. import android.support.v7.app.AppCompatActivity;
  14. import android.os.Bundle;
  15. import android.util.Log;
  16. import android.widget.EditText;
  17. import android.widget.ImageView;
  18. import android.widget.TextView;
  19. import android.widget.Toast;
  20.  
  21. import com.google.android.gms.appindexing.Action;
  22. import com.google.android.gms.appindexing.AppIndex;
  23. import com.google.android.gms.common.ConnectionResult;
  24. import com.google.android.gms.common.api.GoogleApiClient;
  25. import com.google.android.gms.location.LocationServices;
  26. import com.lakimens.lweather.R;
  27. import com.lakimens.lweather.weather.Current;
  28.  
  29. import org.json.JSONException;
  30. import org.json.JSONObject;
  31.  
  32. import java.io.IOException;
  33.  
  34. import butterknife.BindView;
  35. import butterknife.ButterKnife;
  36. import okhttp3.Call;
  37. import okhttp3.Callback;
  38. import okhttp3.OkHttpClient;
  39. import okhttp3.Request;
  40. import okhttp3.Response;
  41.  
  42.  
  43. public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
  44.     public static final String TAG = MainActivity.class.getSimpleName();
  45.  
  46.     @BindView(R.id.locateBTN)
  47.     ImageView mLocateButton;
  48.     @BindView(R.id.locationTV)
  49.     TextView mLocationText;
  50.     @BindView(R.id.tempTV)
  51.     TextView mTempText;
  52.     @BindView(R.id.timeTV)
  53.     TextView mTimeText;
  54.     @BindView(R.id.locationET)
  55.     EditText mLocationEText;
  56.  
  57.     private Current mCurrent;
  58.     private Location mLocation;
  59.     private GoogleApiClient mClient;
  60.     double latitude;
  61.     double longtitude;
  62.  
  63.  
  64.     @Override
  65.     protected void onCreate(Bundle savedInstanceState) {
  66.         super.onCreate(savedInstanceState);
  67.         setContentView(R.layout.activity_main);
  68.         ButterKnife.bind(this);
  69.  
  70.         String APIKey = "HIDDEN FOR OBVIOUS REASONS";
  71.  
  72.         mClient = new GoogleApiClient.Builder(this)
  73.                 .addConnectionCallbacks(this)
  74.                 .addOnConnectionFailedListener(this)
  75.                 .addApi(LocationServices.API)
  76.                 .build();
  77.  
  78.         Uri.Builder builder = new Uri.Builder();
  79.         builder.scheme("https")
  80.                 .authority("api.forecast.io")
  81.                 .appendPath("forecast")
  82.                 .appendPath(APIKey)
  83.                 .appendPath(longtitude + "," + latitude)
  84.                 .appendQueryParameter("units", "si");
  85.         String URL = builder.build().toString();
  86.  
  87.         if (isNetworkAvailible()) {
  88.             OkHttpClient client = new OkHttpClient();
  89.             Request request = new Request.Builder()
  90.                     .url(URL)
  91.                     .build();
  92.             Call call = client.newCall(request);
  93.             call.enqueue(new Callback() {
  94.                 @Override
  95.                 public void onFailure(Call call, IOException e) {
  96.  
  97.                 }
  98.  
  99.                 @Override
  100.                 public void onResponse(Call call, Response response) throws IOException {
  101.  
  102.                     String jsonData = response.body().string();
  103.                     Log.v(TAG, jsonData);
  104.                     if (response.isSuccessful()) {
  105.                         try {
  106.                             mCurrent = getData(jsonData);
  107.                             runOnUiThread(new Runnable() {
  108.                                 @Override
  109.                                 public void run() {
  110.                                     mTempText.setText(longtitude + "," + latitude);
  111.                                 }
  112.                             });
  113.                             Log.i(TAG, "LNG: " + longtitude);
  114.                             Log.i(TAG, "LAT: " + latitude);
  115.  
  116.                         } catch (JSONException e) {
  117.                             Log.e(TAG, "Exception caught:", e);
  118.                         }
  119.                     }
  120.  
  121.                 }
  122.             });
  123.         } else {
  124.             Toast.makeText(this, "Can't get DATA", Toast.LENGTH_SHORT).show();
  125.         }
  126.     }
  127.  
  128.  
  129.     public boolean isNetworkAvailible() {
  130.         ConnectivityManager manager = (ConnectivityManager)
  131.                 getSystemService(Context.CONNECTIVITY_SERVICE);
  132.         NetworkInfo netInfo = manager.getActiveNetworkInfo();
  133.         boolean isAvailible = false;
  134.         if (netInfo != null) {
  135.             isAvailible = true;
  136.         }
  137.         return isAvailible;
  138.     }
  139.  
  140.     private Current getData(String jsonData) throws JSONException {
  141.         JSONObject forecast = new JSONObject(jsonData);
  142.         String timezone = forecast.getString("timezone");
  143.         Log.i(TAG, "JSON" + timezone);
  144.         JSONObject currently = forecast.getJSONObject("currently");
  145.  
  146.         Current Current = new Current();
  147.         Current.setTime(currently.getLong("time"));
  148.         Current.setTemp(currently.getDouble("temperature"));
  149.         Current.setIcon(currently.getString("icon"));
  150.         Current.setPrecip(currently.getDouble("precipProbability"));
  151.         Current.setSummary(currently.getString("summary"));
  152.         Current.setTimeZone(timezone);
  153.  
  154.         Log.d(TAG, Current.getFormattedTime());
  155.  
  156.         return Current;
  157.     }
  158.  
  159.     @Override
  160.     public void onConnected(@Nullable Bundle bundle) {
  161.         if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
  162.                 != PackageManager.PERMISSION_GRANTED
  163.                 && ActivityCompat.checkSelfPermission
  164.                 (this, Manifest.permission.ACCESS_COARSE_LOCATION)
  165.                 != PackageManager.PERMISSION_GRANTED) {
  166.             return;
  167.         }
  168.         mLocation = LocationServices.FusedLocationApi.getLastLocation(mClient);
  169.         if(mLocation != null){
  170.             latitude = mLocation.getLatitude();
  171.             longtitude = mLocation.getLongitude();
  172.         }
  173.         else{
  174.             Toast.makeText(this, "Location not detected", Toast.LENGTH_SHORT);
  175.         }
  176.     }
  177.  
  178.  
  179.     @Override
  180.     public void onConnectionSuspended(int i) {
  181.         Log.i(TAG, "Connection Suspended");
  182.         mClient.connect();
  183.     }
  184.  
  185.     @Override
  186.     public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
  187.         Log.i(TAG, "Connection Failed: " + connectionResult.getErrorCode());
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement