Skip to content

Commit

Permalink
updated version and version code and fixed some crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditya committed Aug 5, 2018
1 parent 710f6ee commit cf4f5be
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 55 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
minSdkVersion 21
//noinspection OldTargetApi
targetSdkVersion 27
versionCode 1
versionName "0.1"
versionCode 7
versionName "0.7"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
2 changes: 1 addition & 1 deletion app/release/output.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":1,"versionName":"0.1","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}]
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":7,"versionName":"0.7","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}]
133 changes: 81 additions & 52 deletions app/src/main/java/com/simple/weatherapp/SetupFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -31,11 +32,16 @@
import java.util.List;
import java.util.Objects;

import static android.content.ContentValues.TAG;

public class SetupFragment extends Fragment {

public static String owmKey, location, units = "metric";
View v;
EditText loc = null;
int unitsID = 0;
boolean PERMISSION_DENIED = false;
String UNITS = "";

@Nullable
@Override
Expand All @@ -50,10 +56,8 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

FusedLocationProviderClient mFusedLocationClient = LocationServices.getFusedLocationProviderClient(Objects.requireNonNull(getActivity()));

final EditText apiKey = v.findViewById(R.id.apiKeyField);
final EditText loc = v.findViewById(R.id.locationField);
loc = v.findViewById(R.id.locationField);
final RadioGroup unitsGroup = v.findViewById(R.id.unitsRadioGroup);
unitsID = unitsGroup.getCheckedRadioButtonId();

Expand Down Expand Up @@ -87,60 +91,14 @@ public void onCheckedChanged(RadioGroup group, int checkedId) {
}
});

if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION}, 0);
} else {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 0);

loc.setVisibility(View.VISIBLE);
setLocation();

Toast.makeText(getActivity(), "Location Permission Denied. Please Enter a manual location.", Toast.LENGTH_SHORT).show();

/*final Intent i = new Intent();
i.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
i.addCategory(Intent.CATEGORY_DEFAULT);
i.setData(Uri.parse("package:" + getActivity().getPackageName()));
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
getActivity().startActivity(i);
Toast.makeText(getActivity(), "App Cannot Work without Location Permission. Please Grant Location Persmission", Toast.LENGTH_SHORT).show();*/
}

mFusedLocationClient.getLastLocation().addOnSuccessListener(getActivity(), new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location loc) {
// Got last known location. In some rare situations this can be null.
if (loc != null) {
List<Address> addressList = null;

Geocoder geocoder = new Geocoder(getActivity());

try {
double latitude = loc.getLatitude();
double longitude = loc.getLongitude();

addressList = geocoder.getFromLocation(latitude, longitude, 1);

} catch (Exception e) {
e.printStackTrace();
}

Address address = Objects.requireNonNull(addressList).get(0);

location = address.getLocality();

if (location.isEmpty()) {
Toast.makeText(getActivity(), "Location Undetectable", Toast.LENGTH_SHORT).show();
}

}
}
});


Button b = v.findViewById(R.id.enterKeyButton);

Expand All @@ -149,11 +107,17 @@ public void onSuccess(Location loc) {
public void onClick(View v) {

owmKey = apiKey.getText().toString();
if (PERMISSION_DENIED) {

location = loc.getText().toString();

}

if (owmKey.equals("")) {

Toast.makeText(getActivity(), "Please enter the API Key", Toast.LENGTH_SHORT).show();


} else if (location.isEmpty()) {

Toast.makeText(getActivity(), "Location Undetectable", Toast.LENGTH_SHORT).show();
Expand Down Expand Up @@ -183,5 +147,70 @@ public void onClick(View v) {
}
});
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

super.onRequestPermissionsResult(requestCode, permissions, grantResults);

switch (requestCode) {
case 0: {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
setLocation();
Log.d(TAG, "permission granted");
} else {

loc.setVisibility(View.VISIBLE);
PERMISSION_DENIED = true;

Toast.makeText(getActivity(), "Location Permission Denied. Please Enter a manual location.", Toast.LENGTH_SHORT).show();
Log.d(TAG, "permission denied");
}
}

}
}

public void setLocation() {

FusedLocationProviderClient mFusedLocationClient = LocationServices.getFusedLocationProviderClient(Objects.requireNonNull(getActivity()));

if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 0);

}
mFusedLocationClient.getLastLocation().addOnSuccessListener(getActivity(), new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location loc) {
// Got last known location. In some rare situations this can be null.
if (loc != null) {
List<Address> addressList = null;

Geocoder geocoder = new Geocoder(getActivity());

try {
double latitude = loc.getLatitude();
double longitude = loc.getLongitude();

addressList = geocoder.getFromLocation(latitude, longitude, 1);

} catch (Exception e) {
e.printStackTrace();
}

Address address = Objects.requireNonNull(addressList).get(0);

location = address.getLocality();

if (location.isEmpty()) {
Toast.makeText(getActivity(), "Location Undetectable", Toast.LENGTH_SHORT).show();
}

}
}
});

}
}

0 comments on commit cf4f5be

Please sign in to comment.