2018-07-28 23:52:02 +07:00
|
|
|
package io.anuke.mindustry;
|
|
|
|
|
2019-09-07 08:21:30 +07:00
|
|
|
import android.app.*;
|
2019-08-31 04:32:49 +07:00
|
|
|
import android.content.*;
|
|
|
|
import android.content.pm.*;
|
|
|
|
import android.net.*;
|
2019-09-07 08:21:30 +07:00
|
|
|
import android.os.Build.*;
|
2019-09-08 01:11:50 +07:00
|
|
|
import android.os.*;
|
2019-08-31 04:32:49 +07:00
|
|
|
import android.provider.Settings.*;
|
|
|
|
import android.telephony.*;
|
|
|
|
import io.anuke.arc.*;
|
|
|
|
import io.anuke.arc.backends.android.surfaceview.*;
|
|
|
|
import io.anuke.arc.files.*;
|
|
|
|
import io.anuke.arc.function.*;
|
|
|
|
import io.anuke.arc.scene.ui.layout.*;
|
|
|
|
import io.anuke.arc.util.*;
|
|
|
|
import io.anuke.arc.util.serialization.*;
|
|
|
|
import io.anuke.mindustry.game.Saves.*;
|
|
|
|
import io.anuke.mindustry.io.*;
|
|
|
|
import io.anuke.mindustry.ui.dialogs.*;
|
2018-07-28 23:52:02 +07:00
|
|
|
|
2019-04-08 20:03:18 +07:00
|
|
|
import java.io.*;
|
2019-08-31 04:32:49 +07:00
|
|
|
import java.lang.System;
|
2018-07-28 23:52:02 +07:00
|
|
|
|
2019-08-27 20:27:09 +07:00
|
|
|
import static io.anuke.mindustry.Vars.*;
|
2018-07-28 23:52:02 +07:00
|
|
|
|
2018-12-28 03:47:17 +07:00
|
|
|
public class AndroidLauncher extends AndroidApplication{
|
2018-07-28 23:52:02 +07:00
|
|
|
public static final int PERMISSION_REQUEST_CODE = 1;
|
|
|
|
boolean doubleScaleTablets = true;
|
|
|
|
FileChooser chooser;
|
2019-08-14 02:14:03 +07:00
|
|
|
Runnable permCallback;
|
2018-07-28 23:52:02 +07:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState){
|
|
|
|
super.onCreate(savedInstanceState);
|
2019-08-31 04:32:49 +07:00
|
|
|
if(doubleScaleTablets && isTablet(this.getContext())){
|
|
|
|
UnitScl.dp.addition = 0.5f;
|
|
|
|
}
|
|
|
|
|
|
|
|
initialize(new ClientLauncher(){
|
2018-07-28 23:52:02 +07:00
|
|
|
|
2019-04-01 00:16:43 +07:00
|
|
|
@Override
|
|
|
|
public void hide(){
|
2019-04-01 05:26:52 +07:00
|
|
|
moveTaskToBack(true);
|
2019-04-01 00:16:43 +07:00
|
|
|
}
|
|
|
|
|
2018-07-28 23:52:02 +07:00
|
|
|
@Override
|
|
|
|
public String getUUID(){
|
|
|
|
try{
|
2018-10-31 06:15:12 +07:00
|
|
|
String s = Secure.getString(getContext().getContentResolver(), Secure.ANDROID_ID);
|
2018-07-28 23:52:02 +07:00
|
|
|
int len = s.length();
|
|
|
|
byte[] data = new byte[len / 2];
|
|
|
|
for(int i = 0; i < len; i += 2){
|
2019-04-08 20:03:18 +07:00
|
|
|
data[i / 2] = (byte)((Character.digit(s.charAt(i), 16) << 4)
|
2018-07-28 23:52:02 +07:00
|
|
|
+ Character.digit(s.charAt(i + 1), 16));
|
|
|
|
}
|
|
|
|
String result = new String(Base64Coder.encode(data));
|
|
|
|
if(result.equals("AAAAAAAAAOA=")) throw new RuntimeException("Bad UUID.");
|
|
|
|
return result;
|
|
|
|
}catch(Exception e){
|
|
|
|
return super.getUUID();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void shareFile(FileHandle file){
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-09-07 08:21:30 +07:00
|
|
|
public void showFileChooser(boolean open, String extension, Consumer<FileHandle> cons){
|
|
|
|
if(VERSION.SDK_INT >= 19){
|
|
|
|
Intent intent = new Intent(open ? Intent.ACTION_OPEN_DOCUMENT : Intent.ACTION_CREATE_DOCUMENT);
|
|
|
|
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
|
|
|
intent.setType("*/*");
|
|
|
|
addResultListener(i -> startActivityForResult(intent, i), (code, in) -> {
|
|
|
|
if(code == Activity.RESULT_OK && in != null && in.getData() != null){
|
|
|
|
Uri uri = in.getData();
|
|
|
|
|
|
|
|
Core.app.post(() -> Core.app.post(() -> cons.accept(new FileHandle(uri.getPath()){
|
|
|
|
@Override
|
|
|
|
public InputStream read(){
|
|
|
|
try{
|
|
|
|
return getContentResolver().openInputStream(uri);
|
|
|
|
}catch(IOException e){
|
|
|
|
throw new ArcRuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public OutputStream write(boolean append){
|
|
|
|
try{
|
|
|
|
return getContentResolver().openOutputStream(uri);
|
|
|
|
}catch(IOException e){
|
|
|
|
throw new ArcRuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})));
|
|
|
|
}
|
|
|
|
});
|
2018-07-28 23:52:02 +07:00
|
|
|
}else{
|
2019-09-07 08:21:30 +07:00
|
|
|
super.showFileChooser(open, extension, cons);
|
2018-07-28 23:52:02 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void beginForceLandscape(){
|
2019-03-16 07:32:08 +07:00
|
|
|
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
|
2018-07-28 23:52:02 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void endForceLandscape(){
|
2019-07-30 12:02:08 +07:00
|
|
|
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
|
2018-07-28 23:52:02 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canDonate(){
|
|
|
|
return true;
|
|
|
|
}
|
2019-09-07 23:04:28 +07:00
|
|
|
}, new AndroidApplicationConfiguration(){{
|
|
|
|
useImmersiveMode = true;
|
|
|
|
depth = 0;
|
|
|
|
hideStatusBar = true;
|
|
|
|
}});
|
2018-07-28 23:52:02 +07:00
|
|
|
checkFiles(getIntent());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-03-25 06:58:51 +07:00
|
|
|
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){
|
2018-07-28 23:52:02 +07:00
|
|
|
if(requestCode == PERMISSION_REQUEST_CODE){
|
|
|
|
for(int i : grantResults){
|
|
|
|
if(i != PackageManager.PERMISSION_GRANTED) return;
|
|
|
|
}
|
|
|
|
if(chooser != null){
|
2019-08-14 02:14:03 +07:00
|
|
|
Core.app.post(chooser::show);
|
|
|
|
}
|
|
|
|
if(permCallback != null){
|
|
|
|
Core.app.post(permCallback);
|
|
|
|
permCallback = null;
|
2018-07-28 23:52:02 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void checkFiles(Intent intent){
|
|
|
|
try{
|
|
|
|
Uri uri = intent.getData();
|
|
|
|
if(uri != null){
|
|
|
|
File myFile = null;
|
|
|
|
String scheme = uri.getScheme();
|
|
|
|
if(scheme.equals("file")){
|
|
|
|
String fileName = uri.getEncodedPath();
|
|
|
|
myFile = new File(fileName);
|
|
|
|
}else if(!scheme.equals("content")){
|
|
|
|
//error
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
boolean save = uri.getPath().endsWith(saveExtension);
|
|
|
|
boolean map = uri.getPath().endsWith(mapExtension);
|
|
|
|
InputStream inStream;
|
|
|
|
if(myFile != null) inStream = new FileInputStream(myFile);
|
|
|
|
else inStream = getContentResolver().openInputStream(uri);
|
2019-07-03 05:52:04 +07:00
|
|
|
Core.app.post(() -> Core.app.post(() -> {
|
2018-07-28 23:52:02 +07:00
|
|
|
if(save){ //open save
|
|
|
|
System.out.println("Opening save.");
|
2018-12-28 03:47:17 +07:00
|
|
|
FileHandle file = Core.files.local("temp-save." + saveExtension);
|
2018-07-28 23:52:02 +07:00
|
|
|
file.write(inStream, false);
|
|
|
|
if(SaveIO.isSaveValid(file)){
|
|
|
|
try{
|
2018-10-08 20:41:01 +07:00
|
|
|
SaveSlot slot = control.saves.importSave(file);
|
2018-07-28 23:52:02 +07:00
|
|
|
ui.load.runLoadSave(slot);
|
|
|
|
}catch(IOException e){
|
2019-09-09 09:32:47 +07:00
|
|
|
ui.showException("save.import.fail", e);
|
2018-07-28 23:52:02 +07:00
|
|
|
}
|
|
|
|
}else{
|
2019-09-09 09:32:47 +07:00
|
|
|
ui.showErrorMessage("$save.import.invalid");
|
2018-07-28 23:52:02 +07:00
|
|
|
}
|
|
|
|
}else if(map){ //open map
|
2019-03-13 00:46:40 +07:00
|
|
|
FileHandle file = Core.files.local("temp-map." + mapExtension);
|
|
|
|
file.write(inStream, false);
|
2018-12-28 03:47:17 +07:00
|
|
|
Core.app.post(() -> {
|
2018-07-28 23:52:02 +07:00
|
|
|
System.out.println("Opening map.");
|
|
|
|
if(!ui.editor.isShown()){
|
|
|
|
ui.editor.show();
|
|
|
|
}
|
2019-03-13 00:46:40 +07:00
|
|
|
ui.editor.beginEditMap(file);
|
2018-07-28 23:52:02 +07:00
|
|
|
});
|
|
|
|
}
|
2019-07-03 05:52:04 +07:00
|
|
|
}));
|
2018-07-28 23:52:02 +07:00
|
|
|
}
|
|
|
|
}catch(IOException e){
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isTablet(Context context){
|
2019-04-08 20:03:18 +07:00
|
|
|
TelephonyManager manager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
|
2019-04-02 05:51:49 +07:00
|
|
|
return manager != null && manager.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE;
|
2018-07-28 23:52:02 +07:00
|
|
|
}
|
|
|
|
}
|