This commit is contained in:
dgis 2019-02-08 19:19:52 +00:00
parent 72ddaacff6
commit aad2426b40

View file

@ -11,12 +11,14 @@ import android.content.SharedPreferences;
import android.content.pm.ActivityInfo; import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.res.AssetManager; import android.content.res.AssetManager;
import android.database.Cursor;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.Color; import android.graphics.Color;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.provider.DocumentsContract;
import android.util.Log; import android.util.Log;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
@ -27,12 +29,14 @@ import android.widget.TextView;
import com.google.android.material.navigation.NavigationView; import com.google.android.material.navigation.NavigationView;
import com.google.android.material.snackbar.Snackbar; import com.google.android.material.snackbar.Snackbar;
import org.emulator.forty.eight.R;
import org.w3c.dom.DocumentType;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
@ -41,6 +45,8 @@ import java.util.Comparator;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -56,6 +62,7 @@ import androidx.appcompat.widget.Toolbar;
import androidx.core.app.ActivityCompat; import androidx.core.app.ActivityCompat;
import androidx.core.content.FileProvider; import androidx.core.content.FileProvider;
import androidx.core.view.GravityCompat; import androidx.core.view.GravityCompat;
import androidx.documentfile.provider.DocumentFile;
import androidx.drawerlayout.widget.DrawerLayout; import androidx.drawerlayout.widget.DrawerLayout;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, SharedPreferences.OnSharedPreferenceChangeListener { public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, SharedPreferences.OnSharedPreferenceChangeListener {
@ -79,6 +86,12 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
private final static int PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 2; private final static int PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 2;
private String kmlMimeType = "application/vnd.google-earth.kml+xml";
private boolean kmlFolderUseDefault = true;
private String kmlFolderURL = "";
private boolean kmFolderChange = true;
private int MRU_ID_START = 10000; private int MRU_ID_START = 10000;
private int MAX_MRU = 5; private int MAX_MRU = 5;
private LinkedHashMap<String, String> mruLinkedHashMap = new LinkedHashMap<String, String>(5, 1.0f, true) { private LinkedHashMap<String, String> mruLinkedHashMap = new LinkedHashMap<String, String>(5, 1.0f, true) {
@ -421,74 +434,168 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
} }
ArrayList<KMLScriptItem> kmlScripts; ArrayList<KMLScriptItem> kmlScripts;
private void extractKMLScripts() { private void extractKMLScripts() {
if(kmlScripts == null) { if(kmlScripts == null || kmFolderChange) {
kmFolderChange = false;
kmlScripts = new ArrayList<>(); kmlScripts = new ArrayList<>();
AssetManager assetManager = getAssets();
String[] calculatorsAssetFilenames = new String[0]; if(kmlFolderUseDefault) {
try { AssetManager assetManager = getAssets();
calculatorsAssetFilenames = assetManager.list("calculators"); String[] calculatorsAssetFilenames = new String[0];
} catch (IOException e) { try {
e.printStackTrace(); calculatorsAssetFilenames = assetManager.list("calculators");
} } catch (IOException e) {
String cKmlType = null; //"S"; e.printStackTrace();
kmlScripts.clear(); }
Pattern patternGlobalTitle = Pattern.compile("\\s*Title\\s+\"(.*)\""); String cKmlType = null; //"S";
Pattern patternGlobalModel = Pattern.compile("\\s*Model\\s+\"(.*)\""); kmlScripts.clear();
Matcher m; Pattern patternGlobalTitle = Pattern.compile("\\s*Title\\s+\"(.*)\"");
for (String calculatorsAssetFilename : calculatorsAssetFilenames) { Pattern patternGlobalModel = Pattern.compile("\\s*Model\\s+\"(.*)\"");
if (calculatorsAssetFilename.toLowerCase().lastIndexOf(".kml") != -1) { Matcher m;
BufferedReader reader = null; for (String calculatorsAssetFilename : calculatorsAssetFilenames) {
try { if (calculatorsAssetFilename.toLowerCase().lastIndexOf(".kml") != -1) {
reader = new BufferedReader(new InputStreamReader(assetManager.open("calculators/" + calculatorsAssetFilename), "UTF-8")); BufferedReader reader = null;
// do reading, usually loop until end of file reading try {
String mLine; reader = new BufferedReader(new InputStreamReader(assetManager.open("calculators/" + calculatorsAssetFilename), "UTF-8"));
boolean inGlobal = false; // do reading, usually loop until end of file reading
String title = null; String mLine;
String model = null; boolean inGlobal = false;
while ((mLine = reader.readLine()) != null) { String title = null;
//process line String model = null;
if (mLine.indexOf("Global") == 0) { while ((mLine = reader.readLine()) != null) {
inGlobal = true; //process line
title = null; if (mLine.indexOf("Global") == 0) {
model = null; inGlobal = true;
continue;
}
if (inGlobal) {
if (mLine.indexOf("End") == 0) {
KMLScriptItem newKMLScriptItem = new KMLScriptItem();
newKMLScriptItem.filename = calculatorsAssetFilename;
newKMLScriptItem.title = title;
newKMLScriptItem.model = model;
kmlScripts.add(newKMLScriptItem);
title = null; title = null;
model = null; model = null;
break; continue;
} }
if (inGlobal) {
if (mLine.indexOf("End") == 0) {
KMLScriptItem newKMLScriptItem = new KMLScriptItem();
newKMLScriptItem.filename = calculatorsAssetFilename;
newKMLScriptItem.title = title;
newKMLScriptItem.model = model;
kmlScripts.add(newKMLScriptItem);
title = null;
model = null;
break;
}
m = patternGlobalTitle.matcher(mLine); m = patternGlobalTitle.matcher(mLine);
if (m.find()) { if (m.find()) {
title = m.group(1); title = m.group(1);
} }
m = patternGlobalModel.matcher(mLine); m = patternGlobalModel.matcher(mLine);
if (m.find()) { if (m.find()) {
model = m.group(1); model = m.group(1);
}
} }
} }
} } catch (IOException e) {
} catch (IOException e) { //log the exception
//log the exception e.printStackTrace();
e.printStackTrace(); } finally {
} finally { if (reader != null) {
if (reader != null) { try {
try { reader.close();
reader.close(); } catch (IOException e) {
} catch (IOException e) { //log the exception
//log the exception }
} }
} }
} }
} }
} else {
Uri kmlFolderUri = Uri.parse(kmlFolderURL);
//DocumentFile.fromTreeUri(this, kmlFolderUri);
//Uri docUri = DocumentsContract.buildDocumentUriUsingTree(kmlFolderUri, DocumentsContract.getTreeDocumentId(kmlFolderUri));
Uri childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(kmlFolderUri, DocumentsContract.getTreeDocumentId(kmlFolderUri));
//String[] calculatorsAssetFilenames = new String[0];
List<String> calculatorsAssetFilenames = new LinkedList<>();
Cursor cursor = getContentResolver().query(childrenUri, new String[]{DocumentsContract.Document.COLUMN_DOCUMENT_ID, DocumentsContract.Document.COLUMN_DISPLAY_NAME, DocumentsContract.Document.COLUMN_MIME_TYPE}, null, null, null);
try {
while (cursor.moveToNext()) {
final String docId = cursor.getString(0);
final String name = cursor.getString(1);
final String mime = cursor.getString(2);
Log.d(TAG, "docId: " + docId + ", name: " + name + ", mime: " + mime);
if(kmlMimeType.equals(mime)) {
calculatorsAssetFilenames.add(docId);
}
}
} finally {
cursor.close();
}
String cKmlType = null; //"S";
kmlScripts.clear();
Pattern patternGlobalTitle = Pattern.compile("\\s*Title\\s+\"(.*)\"");
Pattern patternGlobalModel = Pattern.compile("\\s*Model\\s+\"(.*)\"");
Matcher m;
for (String calculatorsAssetFilename : calculatorsAssetFilenames) {
if (calculatorsAssetFilename.toLowerCase().lastIndexOf(".kml") != -1) {
BufferedReader reader = null;
try {
Uri calculatorsAssetFilenameUri = Uri.parse(calculatorsAssetFilename);
DocumentFile documentFile = DocumentFile.fromSingleUri(this, calculatorsAssetFilenameUri);
InputStream inputStream = getContentResolver().openInputStream(documentFile.getUri());
reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
// do reading, usually loop until end of file reading
String mLine;
boolean inGlobal = false;
String title = null;
String model = null;
while ((mLine = reader.readLine()) != null) {
//process line
if (mLine.indexOf("Global") == 0) {
inGlobal = true;
title = null;
model = null;
continue;
}
if (inGlobal) {
if (mLine.indexOf("End") == 0) {
KMLScriptItem newKMLScriptItem = new KMLScriptItem();
newKMLScriptItem.filename = kmlFolderUseDefault ? calculatorsAssetFilename : kmlFolderURL + "|" + calculatorsAssetFilename;
newKMLScriptItem.title = title;
newKMLScriptItem.model = model;
kmlScripts.add(newKMLScriptItem);
title = null;
model = null;
break;
}
m = patternGlobalTitle.matcher(mLine);
if (m.find()) {
title = m.group(1);
}
m = patternGlobalModel.matcher(mLine);
if (m.find()) {
model = m.group(1);
}
}
}
} catch (IOException e) {
//log the exception
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
//log the exception
}
}
}
}
}
} }
Collections.sort(kmlScripts, new Comparator<KMLScriptItem>() { Collections.sort(kmlScripts, new Comparator<KMLScriptItem>() {
@Override @Override
public int compare(KMLScriptItem lhs, KMLScriptItem rhs) { public int compare(KMLScriptItem lhs, KMLScriptItem rhs) {
@ -560,7 +667,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
// Intent intent = new Intent(Intent.ACTION_GET_CONTENT); // Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
// //intent.setType("file/*"); // //intent.setType("file/*");
// //intent.setType("*/*"); // //intent.setType("*/*");
// intent.setType("application/vnd.google-earth.kml+xml"); // intent.setType(kmlMimeType);
// intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true); // intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
// startActivityForResult(intent, INTENT_PICK_KML_FILE); // startActivityForResult(intent, INTENT_PICK_KML_FILE);
//Intent intent = new Intent(Intent.ACTION_PICK); //Intent intent = new Intent(Intent.ACTION_PICK);
@ -571,14 +678,13 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
// Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); // Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
// intent.addCategory(Intent.CATEGORY_OPENABLE); // intent.addCategory(Intent.CATEGORY_OPENABLE);
// intent.setType("application/vnd.google-earth.kml+xml"); // intent.setType(kmlMimeType);
// intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true); // intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
// startActivityForResult(intent, INTENT_PICK_KML_FILE); // startActivityForResult(intent, INTENT_PICK_KML_FILE);
//https://github.com/googlesamples/android-DirectorySelection //https://github.com/googlesamples/android-DirectorySelection
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, INTENT_PICK_KML_FOLDER); startActivityForResult(intent, INTENT_PICK_KML_FOLDER);
} else { } else {
String kmlScriptFilename = kmlScripts.get(which).filename; String kmlScriptFilename = kmlScripts.get(which).filename;
newFileFromKML(kmlScriptFilename); newFileFromKML(kmlScriptFilename);
@ -837,12 +943,15 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
} }
case INTENT_SETTINGS: case INTENT_SETTINGS:
break; break;
// case INTENT_PICK_KML_FILE: // case INTENT_PICK_KML_FILE: {
// Log.d(TAG, "onActivityResult INTENT_PICK_KML_FILE " + url); // Log.d(TAG, "onActivityResult INTENT_PICK_KML_FILE " + url);
// String filePath = Utils.getFilePath(this, url); //// String filePath = Utils.getFilePath(this, url);
// newFileFromKML(filePath); //// newFileFromKML(filePath);
// DocumentFile documentFile = DocumentFile.fromSingleUri(this, uri);
// DocumentFile parentDocumentFile = documentFile.getParentFile();
// break; // break;
case INTENT_PICK_KML_FOLDER: // }
case INTENT_PICK_KML_FOLDER: {
Log.d(TAG, "onActivityResult INTENT_PICK_KML_FOLDER " + url); Log.d(TAG, "onActivityResult INTENT_PICK_KML_FOLDER " + url);
SharedPreferences.Editor editor = sharedPreferences.edit(); SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("settings_kml_default", false); editor.putBoolean("settings_kml_default", false);
@ -850,9 +959,11 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
editor.apply(); editor.apply();
makeUriPersistableReadOnly(data, uri); makeUriPersistableReadOnly(data, uri);
String filePath = Utils.getFilePath(this, url); OnFileNew();
newFileFromKML(filePath); // String filePath = Utils.getFilePath(this, url);
// newFileFromKML(filePath);
break; break;
}
default: default:
break; break;
} }
@ -1123,13 +1234,14 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
case "settings_kml": case "settings_kml":
case "settings_kml_default": case "settings_kml_default":
case "settings_kml_folder": case "settings_kml_folder":
boolean useDefaultKMLFolder = sharedPreferences.getBoolean("settings_kml_default", true); kmlFolderUseDefault = sharedPreferences.getBoolean("settings_kml_default", true);
if(!useDefaultKMLFolder) { if(!kmlFolderUseDefault) {
String kmlFolderURL = sharedPreferences.getString("settings_kml_folder", ""); kmlFolderURL = sharedPreferences.getString("settings_kml_folder", "");
// https://github.com/googlesamples/android-DirectorySelection // https://github.com/googlesamples/android-DirectorySelection
// https://stackoverflow.com/questions/44185477/intent-action-open-document-tree-doesnt-seem-to-return-a-real-path-to-drive/44185706 // https://stackoverflow.com/questions/44185477/intent-action-open-document-tree-doesnt-seem-to-return-a-real-path-to-drive/44185706
// https://stackoverflow.com/questions/26744842/how-to-use-the-new-sd-card-access-api-presented-for-android-5-0-lollipop // https://stackoverflow.com/questions/26744842/how-to-use-the-new-sd-card-access-api-presented-for-android-5-0-lollipop
} }
kmFolderChange = true;
break; break;
case "settings_port1": case "settings_port1":