Skip to content

Commit

Permalink
Fixed a few deprecations (sky-map-team#356)
Browse files Browse the repository at this point in the history
* Convert preference to AndroidX preference

* Fix a few deprecations

* Use ContextCompat.getSystemService
  • Loading branch information
TacoTheDank authored Oct 5, 2020
1 parent b35c34e commit fc6f59b
Show file tree
Hide file tree
Showing 15 changed files with 97 additions and 75 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ android {

dependencies {
// Android support
implementation 'androidx.annotation:annotation:1.0.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.preference:preference:1.1.1'

// Third-party
implementation 'com.google.guava:guava:19.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

package com.google.android.stardroid.test.util;

import junit.framework.Assert;
import org.junit.Assert;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import com.google.android.stardroid.base.Provider;
import com.google.android.stardroid.base.VisibleForTesting;

import junit.framework.Assert;
import org.junit.Assert;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import android.hardware.SensorManager;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.preference.PreferenceManager;
import android.util.Log;

import androidx.core.content.ContextCompat;
import androidx.preference.PreferenceManager;

import com.google.android.stardroid.control.AstronomerModel;
import com.google.android.stardroid.control.AstronomerModelImpl;
import com.google.android.stardroid.control.MagneticDeclinationCalculator;
Expand Down Expand Up @@ -74,7 +76,7 @@ SharedPreferences provideSharedPreferences() {
@Provides
@Singleton
LocationManager provideLocationManager() {
return (LocationManager) app.getSystemService(Context.LOCATION_SERVICE);
return ContextCompat.getSystemService(app, LocationManager.class);
}

@Provides
Expand Down Expand Up @@ -125,13 +127,13 @@ Resources provideResources() {
@Provides
@Singleton
SensorManager provideSensorManager() {
return (SensorManager) app.getSystemService(Context.SENSOR_SERVICE);
return ContextCompat.getSystemService(app, SensorManager.class);
}

@Provides
@Singleton
ConnectivityManager provideConnectivityManager() {
return (ConnectivityManager) app.getSystemService(Context.CONNECTIVITY_SERVICE);
return ContextCompat.getSystemService(app, ConnectivityManager.class);
}

@Provides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
import android.hardware.SensorManager;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;

import androidx.core.content.pm.PackageInfoCompat;
import androidx.preference.PreferenceManager;

import com.google.android.stardroid.layers.LayerManager;
import com.google.android.stardroid.util.Analytics;
import com.google.android.stardroid.util.AnalyticsInterface;
Expand Down Expand Up @@ -157,11 +159,11 @@ public String getVersionName() {
/**
* Returns the build number for Sky Map.
*/
public int getVersion() {
public long getVersion() {
PackageManager packageManager = getPackageManager();
try {
PackageInfo info = packageManager.getPackageInfo(this.getPackageName(), 0);
return info.versionCode;
return PackageInfoCompat.getLongVersionCode(info);
} catch (NameNotFoundException e) {
Log.e(TAG, "Unable to obtain package info");
return -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import android.app.FragmentManager;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Expand All @@ -42,6 +41,8 @@
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.content.ContextCompat;

import com.google.android.stardroid.ApplicationConstants;
import com.google.android.stardroid.R;
import com.google.android.stardroid.activities.dialogs.EulaDialogFragment;
Expand Down Expand Up @@ -236,8 +237,10 @@ public void setNightMode(boolean nightMode1) {
activityLightLevelManager = new ActivityLightLevelManager(activityLightLevelChanger,
sharedPreferences);

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, TAG);
PowerManager pm = ContextCompat.getSystemService(this, PowerManager.class);
if (pm != null) {
wakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, TAG);
}

// Were we started as the result of a search?
Intent intent = getIntent();
Expand All @@ -250,7 +253,7 @@ public void setNightMode(boolean nightMode1) {
}

private void checkForSensorsAndMaybeWarn() {
SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
SensorManager sensorManager = ContextCompat.getSystemService(this, SensorManager.class);
if (sensorManager != null && sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null
&& sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD) != null) {
Log.i(TAG, "Minimum sensors present");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.EditTextPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;

import com.google.android.stardroid.ApplicationConstants;
import com.google.android.stardroid.R;
import com.google.android.stardroid.StardroidApplication;
Expand All @@ -44,13 +45,13 @@
/**
* Edit the user's preferences.
*/
public class EditSettingsActivity extends PreferenceActivity {
public class EditSettingsActivity extends AppCompatActivity {
private MyPreferenceFragment preferenceFragment;

public static class MyPreferenceFragment extends PreferenceFragment {

public static class MyPreferenceFragment extends PreferenceFragmentCompat {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.preference_screen);
}
}
Expand All @@ -75,7 +76,7 @@ public void onCreate(Bundle savedInstanceState) {
PreferenceManager.getDefaultSharedPreferences(this));
geocoder = new Geocoder(this);
preferenceFragment = new MyPreferenceFragment();
getFragmentManager().beginTransaction().replace(android.R.id.content,
getSupportFragmentManager().beginTransaction().replace(android.R.id.content,
preferenceFragment).commit();

}
Expand All @@ -86,40 +87,48 @@ public void onStart() {
final Preference locationPreference = preferenceFragment.findPreference(LOCATION);
Preference latitudePreference = preferenceFragment.findPreference(LATITUDE);
Preference longitudePreference = preferenceFragment.findPreference(LONGITUDE);
locationPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

public boolean onPreferenceChange(Preference preference, Object newValue) {
Log.d(TAG, "Place to be updated to " + newValue);
return setLatLongFromPlace(newValue.toString());
}
});

latitudePreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

public boolean onPreferenceChange(Preference preference, Object newValue) {
((EditTextPreference) locationPreference).setText("");
return true;
}
});

longitudePreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

public boolean onPreferenceChange(Preference preference, Object newValue) {
((EditTextPreference) locationPreference).setText("");
return true;
}
});
if (locationPreference != null) {
locationPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

public boolean onPreferenceChange(Preference preference, Object newValue) {
Log.d(TAG, "Place to be updated to " + newValue);
return setLatLongFromPlace(newValue.toString());
}
});
}

if (latitudePreference != null) {
latitudePreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

public boolean onPreferenceChange(Preference preference, Object newValue) {
((EditTextPreference) locationPreference).setText("");
return true;
}
});
}

if (longitudePreference != null) {
longitudePreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

public boolean onPreferenceChange(Preference preference, Object newValue) {
((EditTextPreference) locationPreference).setText("");
return true;
}
});
}

Preference gyroPreference = preferenceFragment.findPreference(
ApplicationConstants.SHARED_PREFERENCE_DISABLE_GYRO);
gyroPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

public boolean onPreferenceChange(Preference preference, Object newValue) {
Log.d(TAG, "Toggling gyro preference " + newValue);
enableNonGyroSensorPrefs(((Boolean) newValue));
return true;
}
});
if (gyroPreference != null) {
gyroPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

public boolean onPreferenceChange(Preference preference, Object newValue) {
Log.d(TAG, "Toggling gyro preference " + newValue);
enableNonGyroSensorPrefs(((Boolean) newValue));
return true;
}
});
}

enableNonGyroSensorPrefs(
sharedPreferences.getBoolean(ApplicationConstants.SHARED_PREFERENCE_DISABLE_GYRO,
Expand Down Expand Up @@ -177,10 +186,14 @@ protected boolean setLatLongFromPlace(String place) {
}

private void setLatLong(double latitude, double longitude) {
EditTextPreference latPreference = (EditTextPreference) preferenceFragment.findPreference(LATITUDE);
EditTextPreference longPreference = (EditTextPreference) preferenceFragment.findPreference(LONGITUDE);
latPreference.setText(Double.toString(latitude));
longPreference.setText(Double.toString(longitude));
EditTextPreference latPreference = preferenceFragment.findPreference(LATITUDE);
EditTextPreference longPreference = preferenceFragment.findPreference(LONGITUDE);
if (latPreference != null) {
latPreference.setText(Double.toString(latitude));
}
if (longPreference != null) {
longPreference.setText(Double.toString(longitude));
}
String message = String.format(getString(R.string.location_place_found), latitude, longitude);
Log.d(TAG, message);
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.preference.PreferenceManager;

import com.google.android.stardroid.R;
import com.google.android.stardroid.StardroidApplication;
import com.google.android.stardroid.activities.util.ActivityLightLevelChanger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -27,6 +26,8 @@
import android.widget.ImageView;
import android.widget.TextView;

import androidx.preference.PreferenceManager;

import com.google.android.stardroid.R;
import com.google.android.stardroid.activities.util.ActivityLightLevelChanger;
import com.google.android.stardroid.activities.util.ActivityLightLevelManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ private void maybeShowWhatsNewAndEnd() {
@Override
public void dialogClosed() {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(ApplicationConstants.READ_WHATS_NEW_PREF_VERSION, app.getVersion());
editor.putLong(ApplicationConstants.READ_WHATS_NEW_PREF_VERSION, app.getVersion());
editor.commit();
launchSkyMap();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {

DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
if (whichButton == Dialog.BUTTON2) {
if (whichButton == Dialog.BUTTON_NEGATIVE) {
Log.d(TAG, "Many search results Dialog closed with cancel");
dialog.dismiss();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;

import androidx.preference.PreferenceManager;

import com.google.android.stardroid.R;
import com.google.android.stardroid.units.LatLong;
import com.google.android.stardroid.util.MiscUtil;
Expand Down Expand Up @@ -172,14 +173,14 @@ private Builder getSwitchOnGPSDialog() {
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setTitle(R.string.location_offer_to_enable_gps_title);
dialog.setMessage(R.string.location_offer_to_enable);
dialog.setPositiveButton(android.R.string.yes, new OnClickListener() {
dialog.setPositiveButton(android.R.string.ok, new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.d(TAG, "Sending to editor location prefs page");
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
context.startActivity(intent);
}
});
dialog.setNegativeButton(android.R.string.no, new OnClickListener() {
dialog.setNegativeButton(android.R.string.cancel, new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.d(TAG, "User doesn't want to enable location.");
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ private int addLabelsInternal(GL10 gl, Paint textPaint, boolean drawToCanvas,
}

private void beginAdding(GL10 gl) {
Bitmap.Config config = mFullColor ? Bitmap.Config.ARGB_4444 : Bitmap.Config.ALPHA_8;
Bitmap.Config config = mFullColor ? Bitmap.Config.ARGB_8888 : Bitmap.Config.ALPHA_8;
mBitmap = Bitmap.createBitmap(mStrikeWidth, mStrikeHeight, config);
mCanvas = new Canvas(mBitmap);
mBitmap.eraseColor(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
package com.google.android.stardroid.touch;

import android.content.Context;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;

import com.google.android.stardroid.control.AstronomerModel;
import com.google.android.stardroid.control.ControllerGroup;
Expand All @@ -41,9 +40,8 @@ public class MapMover implements
public MapMover(AstronomerModel model, ControllerGroup controllerGroup, Context context) {
this.model = model;
this.controllerGroup = controllerGroup;
Display display = ((WindowManager) context.getSystemService(
Context.WINDOW_SERVICE)).getDefaultDisplay();
int screenLongSize = display.getHeight();
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int screenLongSize = metrics.heightPixels;
Log.i(TAG, "Screen height is " + screenLongSize + " pixels.");
sizeTimesRadiansToDegrees = screenLongSize * Geometry.RADIANS_TO_DEGREES;
}
Expand Down
Loading

0 comments on commit fc6f59b

Please sign in to comment.