Search in sources :

Example 6 with MyMap

use of com.junjunguo.pocketmaps.model.MyMap in project PocketMaps by junjunguo.

the class DownloadMapActivity method getMapsFromJSsources.

/**
 * Read all maps data from server.
 * @param mapNameFilter MapName or null for all.
 * @return list of MyMap
 */
public static List<MyMap> getMapsFromJSsources(String mapNameFilter, OnProgressListener task) {
    ArrayList<MyMap> maps = new ArrayList<>();
    try {
        String jsonDirUrl = Variable.getVariable().getMapUrlJSON();
        String jsonFileUrl = jsonDirUrl + "/map_url_json";
        String jsonContent = DownloadFiles.getDownloader().downloadTextfile(jsonFileUrl);
        task.onProgress(50);
        JSONObject jsonObj = new JSONObject(jsonContent);
        if (jsonObj.has("maps-" + MyMap.MAP_VERSION) && jsonObj.has("maps-" + MyMap.MAP_VERSION + "-path")) {
            String mapsPath = jsonObj.getString("maps-" + MyMap.MAP_VERSION + "-path");
            JSONArray jsonList = jsonObj.getJSONArray("maps-" + MyMap.MAP_VERSION);
            for (int i = 0; i < jsonList.length(); i++) {
                JSONObject o = jsonList.getJSONObject(i);
                String name = o.getString("name");
                if (mapNameFilter != null && !mapNameFilter.equals(name)) {
                    continue;
                }
                String size = o.getString("size");
                String time = o.getString("time");
                MyMap curMap = new MyMap(name, size, time, jsonDirUrl + "/" + mapsPath + "/" + time + "/");
                maps.add(curMap);
                float progress = i;
                progress = i / jsonList.length();
                progress = progress * 50.0f;
                task.onProgress(50 + (int) progress);
            }
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return maps;
}
Also used : JSONObject(org.json.JSONObject) MyMap(com.junjunguo.pocketmaps.model.MyMap) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException)

Example 7 with MyMap

use of com.junjunguo.pocketmaps.model.MyMap in project PocketMaps by junjunguo.

the class DownloadMapActivity method onClickMapNow.

/**
 * download map
 *
 * @param view     View
 * @param position item position
 */
private void onClickMapNow(View view, int position, TextView tv) {
    MyMap myMap = myDownloadAdapter.getItem(position);
    if (myMap.getStatus() == MyMap.DlStatus.Downloading || myMap.getStatus() == MyMap.DlStatus.Unzipping) {
        logUser("Already downloading!");
        return;
    } else if (myMap.getStatus() == MyMap.DlStatus.Complete) {
        logUser("Already downloaded!");
        return;
    }
    tv.setText("downloading...");
    refreshMapEntry(myMap, myDownloadAdapter);
    myMap.setStatus(MyMap.DlStatus.Downloading);
    DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    Request request = new Request(Uri.parse(myMap.getUrl()));
    File destFile = MyMap.getMapFile(myMap, MyMap.MapFileType.DlMapFile);
    request.setDestinationUri(Uri.fromFile(destFile));
    long enqueueId = dm.enqueue(request);
    File idFile = MyMap.getMapFile(myMap, MyMap.MapFileType.DlIdFile);
    IO.writeToFile("" + enqueueId, idFile, false);
    BroadcastReceiver br = createBroadcastReceiver(myDownloadAdapter, myMap, enqueueId);
    registerReceiver(br, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    receiverList.add(br);
}
Also used : IntentFilter(android.content.IntentFilter) MyMap(com.junjunguo.pocketmaps.model.MyMap) Request(android.app.DownloadManager.Request) BroadcastReceiver(android.content.BroadcastReceiver) DownloadManager(android.app.DownloadManager) File(java.io.File)

Example 8 with MyMap

use of com.junjunguo.pocketmaps.model.MyMap in project PocketMaps by junjunguo.

the class MainActivity method continueActivity.

boolean continueActivity() {
    if (activityLoaded) {
        return true;
    }
    String sPermission = android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
    if (!Permission.checkPermission(sPermission, this)) {
        String sPermission2 = android.Manifest.permission.ACCESS_FINE_LOCATION;
        Permission.startRequest(new String[] { sPermission, sPermission2 }, true, this);
        return false;
    }
    Variable.getVariable().setContext(getApplicationContext());
    // set status bar
    new SetStatusBarColor().setStatusBarColor(findViewById(R.id.statusBarBackgroundMain), getResources().getColor(R.color.my_primary_dark), this);
    File defMapsDir = getDefaultBaseDirectory(this);
    if (defMapsDir == null) {
        return false;
    }
    Variable.getVariable().setBaseFolder(defMapsDir.getPath());
    if (!Variable.getVariable().getMapsFolder().exists()) {
        Variable.getVariable().getMapsFolder().mkdirs();
    }
    boolean loadSuccess = Variable.getVariable().loadVariables();
    activateAddBtn();
    activateRecyclerView(new ArrayList<MyMap>());
    generateList();
    // vh = null;
    changeMap = getIntent().getBooleanExtra("com.junjunguo.pocketmaps.activities.MapActivity.SELECTNEWMAP", true);
    // start map activity if load succeed
    if (loadSuccess && !changeMap) {
        startMapActivity();
    }
    activityLoaded = true;
    return true;
}
Also used : MyMap(com.junjunguo.pocketmaps.model.MyMap) SetStatusBarColor(com.junjunguo.pocketmaps.util.SetStatusBarColor) File(java.io.File)

Example 9 with MyMap

use of com.junjunguo.pocketmaps.model.MyMap in project PocketMaps by junjunguo.

the class MainActivity method deleteItemHandler.

/**
 * swipe to right or left to delete item & AlertDialog to confirm
 */
private void deleteItemHandler() {
    OnItemClickListener l = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            MyMap mm = mapAdapter.remove(position);
            Variable.getVariable().removeLocalMap(mm);
            File mapsFolder = MyMap.getMapFile(mm, MyMap.MapFileType.MapFolder);
            recursiveDelete(mapsFolder);
            mm.setStatus(MyMap.DlStatus.On_server);
            log("RecursiveDelete: " + mm.getMapName());
        }
    };
    addDeleteItemHandler(this, mapsRV, l);
}
Also used : OnItemClickListener(android.widget.AdapterView.OnItemClickListener) MyMap(com.junjunguo.pocketmaps.model.MyMap) AdapterView(android.widget.AdapterView) View(android.view.View) AdapterView(android.widget.AdapterView) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) File(java.io.File)

Example 10 with MyMap

use of com.junjunguo.pocketmaps.model.MyMap in project PocketMaps by junjunguo.

the class MyDownloadAdapter method remove.

/**
 * remove item at the given position
 *
 * @param position index
 */
public MyMap remove(int position) {
    MyMap mm = null;
    if (position >= 0 && position < getItemCount()) {
        mm = myMaps.remove(position);
        notifyItemRemoved(position);
    }
    return mm;
}
Also used : MyMap(com.junjunguo.pocketmaps.model.MyMap)

Aggregations

MyMap (com.junjunguo.pocketmaps.model.MyMap)12 File (java.io.File)5 ActivityNotFoundException (android.content.ActivityNotFoundException)2 SetStatusBarColor (com.junjunguo.pocketmaps.util.SetStatusBarColor)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 JSONException (org.json.JSONException)2 DownloadManager (android.app.DownloadManager)1 Request (android.app.DownloadManager.Request)1 BroadcastReceiver (android.content.BroadcastReceiver)1 IntentFilter (android.content.IntentFilter)1 ActionBar (android.support.v7.app.ActionBar)1 RecyclerView (android.support.v7.widget.RecyclerView)1 View (android.view.View)1 AdapterView (android.widget.AdapterView)1 OnItemClickListener (android.widget.AdapterView.OnItemClickListener)1 TextView (android.widget.TextView)1 MapUnzip (com.junjunguo.pocketmaps.downloader.MapUnzip)1 FilenameFilter (java.io.FilenameFilter)1 URL (java.net.URL)1