v0.6.3: refactored some android interaction out of the core module

This commit is contained in:
Evan Debenham
2017-12-26 23:06:10 -05:00
parent c290e5fe47
commit e828148ebc
8 changed files with 184 additions and 108 deletions

View File

@@ -46,6 +46,7 @@ import com.watabou.input.Touchscreen;
import com.watabou.noosa.audio.Music;
import com.watabou.noosa.audio.Sample;
import com.watabou.utils.BitmapCache;
import com.watabou.utils.DeviceCompat;
import com.watabou.utils.SystemTime;
import java.util.ArrayList;
@@ -133,10 +134,10 @@ public class Game extends Activity implements GLSurfaceView.Renderer, View.OnTou
view = new GLSurfaceView( this );
view.setEGLContextClientVersion( 2 );
//Versions of android below 4.1 are forced to RGB 565 for performance reasons.
//Older devices are forced to RGB 565 for performance reasons.
//Otherwise try to use RGB888 for best quality, but use RGB565 if it is what's available.
view.setEGLConfigChooser( new ScreenConfigChooser(
Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN,
DeviceCompat.legacyDevice(),
false ));
view.setRenderer( this );

View File

@@ -21,9 +21,11 @@
package com.watabou.noosa.audio;
import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Build;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
@@ -158,4 +160,13 @@ public enum Music implements MediaPlayer.OnPreparedListener, MediaPlayer.OnError
super.onCallStateChanged(state, incomingNumber);
}
};
public static void setMuteListener(){
//versions lower than this require READ_PHONE_STATE permission
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
TelephonyManager mgr =
(TelephonyManager) Game.instance.getSystemService(Activity.TELEPHONY_SERVICE);
mgr.listen(Music.callMute, PhoneStateListener.LISTEN_CALL_STATE);
}
}
}

View File

@@ -0,0 +1,44 @@
/*
* Pixel Dungeon
* Copyright (C) 2012-2015 Oleg Dolya
*
* Shattered Pixel Dungeon
* Copyright (C) 2014-2017 Evan Debenham
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package com.watabou.utils;
import android.os.Build;
public class DeviceCompat {
public static boolean supportsFullScreen(){
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
}
public static boolean legacyDevice(){
return Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN;
}
public static boolean supportsGamesServices(){
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;
}
public static boolean usesISO_8859_1(){
return Build.VERSION.SDK_INT == Build.VERSION_CODES.FROYO;
}
}

View File

@@ -0,0 +1,121 @@
/*
* Pixel Dungeon
* Copyright (C) 2012-2015 Oleg Dolya
*
* Shattered Pixel Dungeon
* Copyright (C) 2014-2017 Evan Debenham
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package com.watabou.utils;
import android.content.SharedPreferences;
import android.os.Build;
import com.watabou.noosa.Game;
public class GameSettings {
private static SharedPreferences prefs;
private static SharedPreferences get() {
if (prefs == null) {
prefs = Game.instance.getPreferences( Game.MODE_PRIVATE );
}
return prefs;
}
public static boolean contains( String key ){
return get().contains( key );
}
public static int getInt( String key, int defValue ) {
return getInt(key, defValue, Integer.MIN_VALUE, Integer.MAX_VALUE);
}
public static int getInt( String key, int defValue, int min, int max ) {
try {
int i = get().getInt( key, defValue );
if (i < min || i > max){
int val = (int)GameMath.gate(min, i, max);
put(key, val);
return val;
} else {
return i;
}
} catch (ClassCastException e) {
//ShatteredPixelDungeon.reportException(e);
put(key, defValue);
return defValue;
}
}
public static boolean getBoolean( String key, boolean defValue ) {
try {
return get().getBoolean(key, defValue);
} catch (ClassCastException e) {
//ShatteredPixelDungeon.reportException(e);
put(key, defValue);
return defValue;
}
}
public static String getString( String key, String defValue ) {
return getString(key, defValue, Integer.MAX_VALUE);
}
public static String getString( String key, String defValue, int maxLength ) {
try {
String s = get().getString( key, defValue );
if (s != null && s.length() > maxLength) {
put(key, defValue);
return defValue;
} else {
return s;
}
} catch (ClassCastException e) {
//ShatteredPixelDungeon.reportException(e);
put(key, defValue);
return defValue;
}
}
//android 2.3+ supports apply, which is asyncronous, much nicer
public static void put( String key, int value ) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
get().edit().putInt(key, value).apply();
} else {
get().edit().putInt(key, value).commit();
}
}
public static void put( String key, boolean value ) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
get().edit().putBoolean(key, value).apply();
} else {
get().edit().putBoolean(key, value).commit();
}
}
public static void put( String key, String value ) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
get().edit().putString(key, value).apply();
} else {
get().edit().putString(key, value).commit();
}
}
}