Example usage for android.app ProgressDialog setProgress

List of usage examples for android.app ProgressDialog setProgress

Introduction

In this page you can find the example usage for android.app ProgressDialog setProgress.

Prototype

public void setProgress(int value) 

Source Link

Document

Sets the current progress.

Usage

From source file:Main.java

public static File copy1(Context context, String filename, String destfilename, ProgressDialog pd) {

    try {/*from w  w w . j  a  v a  2s  . co  m*/
        InputStream in = context.getAssets().open(filename);
        int max = in.available();
        if (pd != null) {
            pd.setMax(max);
        }

        File file = new File(destfilename);
        OutputStream out = new FileOutputStream(file);
        byte[] byt = new byte[1024];
        int len = 0;
        int total = 0;
        while ((len = in.read(byt)) != -1) {
            out.write(byt, 0, len);
            total += len;
            if (pd != null) {
                pd.setProgress(total);
            }
        }
        out.flush();
        out.close();
        in.close();

        return file;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.nextgis.mobile.map.LocalGeoJsonLayer.java

private static List<Feature> geoJSONFeaturesToFeatures(JSONArray geoJSONFeatures, boolean isWGS84,
        Context context, ProgressDialog progressDialog) throws JSONException {

    List<Feature> features = new ArrayList<Feature>();
    List<Field> fields = new ArrayList<Field>();
    int geometryType = GTNone;

    progressDialog.setMessage(context.getString(R.string.message_loading_progress));
    progressDialog.setMax(geoJSONFeatures.length());
    for (int i = 0; i < geoJSONFeatures.length(); i++) {
        progressDialog.setProgress(i);
        JSONObject jsonFeature = geoJSONFeatures.getJSONObject(i);

        //get geometry
        JSONObject jsonGeometry = jsonFeature.getJSONObject(GEOJSON_GEOMETRY);
        GeoGeometry geometry = GeoGeometry.fromJson(jsonGeometry);

        if (geometryType == GTNone) {
            geometryType = geometry.getType();
        } else if (!Geo.isGeometryTypeSame(geometryType, geometry.getType())) {
            //skip different geometry type
            continue;
        }//www  . jav a 2 s . c  o m

        //reproject if needed
        if (isWGS84) {
            geometry.setCRS(CRS_WGS84);
            geometry.project(CRS_WEB_MERCATOR);
        } else {
            geometry.setCRS(CRS_WEB_MERCATOR);
        }

        Feature feature = new Feature(fields);
        feature.setGeometry(geometry);
        //TODO: add to RTree for fast spatial queries

        //normalize attributes
        JSONObject jsonAttributes = jsonFeature.getJSONObject(GEOJSON_PROPERTIES);
        Iterator<String> iter = jsonAttributes.keys();
        while (iter.hasNext()) {
            String key = iter.next();
            Object value = jsonAttributes.get(key);

            int nType = -1;
            //check type
            if (value instanceof Integer || value instanceof Long) {
                nType = FTInteger;
            } else if (value instanceof Double || value instanceof Float) {
                nType = FTReal;
            } else if (value instanceof Date) {
                nType = FTDateTime;
            } else if (value instanceof String) {
                nType = FTString;
            } else if (value instanceof JSONObject) {
                nType = -1;
                //the some list - need to check it type FTIntegerList, FTRealList, FTStringList
            }

            int nField = -1;
            for (int j = 0; j < fields.size(); j++) {
                if (fields.get(j).getFieldName().equals(key)) {
                    nField = j;
                }
            }

            if (nField == -1) { //add new field
                Field field = new Field(key, key, nType);
                nField = fields.size();
                fields.add(field);
            }

            feature.setField(nField, value);
        }

        features.add(feature);
    }

    return features;
}

From source file:com.github.yuukis.businessmap.app.ProgressDialogFragment.java

public void updateProgress(int value) {
    ProgressDialog dialog = (ProgressDialog) getDialog();
    if (dialog != null) {
        dialog.setProgress(value);
    }/*from   ww w  .j a v  a 2s .co  m*/
}

From source file:id.nci.stm_9.ProgressDialogFragment.java

/**
 * Updates progress of dialog/*from  ww w .jav a2  s  .c  o  m*/
 * 
 * @param messageId
 * @param progress
 * @param max
 */
public void setProgress(int progress, int max) {
    ProgressDialog dialog = (ProgressDialog) getDialog();

    dialog.setProgress(progress);
    dialog.setMax(max);
}

From source file:id.nci.stm_9.ProgressDialogFragment.java

/**
 * Updates progress of dialog// w  w  w.  j  a va  2s  .  co  m
 * 
 * @param messageId
 * @param progress
 * @param max
 */
public void setProgress(String message, int progress, int max) {
    ProgressDialog dialog = (ProgressDialog) getDialog();

    dialog.setMessage(message);
    dialog.setProgress(progress);
    dialog.setMax(max);
}

From source file:org.pixmob.freemobile.netstat.ui.ExportDialogFragment.java

public void update(int current, int total) {
    final ProgressDialog dialog = (ProgressDialog) getDialog();
    if (dialog != null) {
        dialog.setIndeterminate(false);/*from  w w  w  .j a va  2  s  . com*/
        dialog.setMax(total);
        dialog.setProgress(current);
    }
}

From source file:org.openbmap.activities.ExportGpxTaskFragment.java

/**
 * Restores previously retained progress dialog state
 * @param progressDialog/*from  ww  w . j av  a2 s  . c o  m*/
 */
public void restoreProgress(final ProgressDialog progressDialog) {
    progressDialog.setTitle(mTitle);
    progressDialog.setMessage(mMessage);
    progressDialog.setProgress(mProgress);
}

From source file:it.scoppelletti.mobilepower.app.ProgressDialogFragment.java

/**
 * Imposta la percentuale di avanzamento.
 * //from  w  w w.j  a  va  2 s  .c om
 * @param value Percentuale.
 */
private void setProgress(int value) {
    ProgressDialog dlg = (ProgressDialog) getDialog();

    dlg.setProgress(value);
}

From source file:be.ac.ucl.lfsab1509.llncampus.UCLouvain.java

/**
 * Launch the download of the courses list and store them in the database.
 * /*from   ww  w  .j a  v  a 2 s .co m*/
 * @param context
 *          Application context.
 * @param username
 *          UCL global user identifier.
 * @param password
 *          UCL password.
 * @param end
 *          Runnable to be executed at the end of the download.
 * @param mHandler
 *          Handler to manage messages and threads.
 *          
 */
public static void downloadCoursesFromUCLouvain(final LLNCampusActivity context, final String username,
        final String password, final Runnable end, final Handler mHandler) {
    Time t = new Time();
    t.setToNow();
    int year = t.year;
    // A new academic year begin in September (8th month on 0-based count).
    if (t.month < 8) {
        year--;
    }
    final int academicYear = year;

    mHandler.post(new Runnable() {

        public void run() {

            final ProgressDialog mProgress = new ProgressDialog(context);
            mProgress.setCancelable(false);
            mProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mProgress.setMax(100);
            mProgress.setMessage(context.getString(R.string.connection));
            mProgress.show();

            new Thread(new Runnable() {
                /**
                 * Set the progress to the value n and show the message nextStep
                 * @param n
                 *          The progress value.
                 * @param nextStep
                 *          The message to show (indicate the next step to be processed).
                 */
                public void progress(final int n, final String nextStep) {
                    mHandler.post(new Runnable() {
                        public void run() {
                            mProgress.setProgress(n);
                            mProgress.setMessage(nextStep);
                        }
                    });
                    Log.d("UCLouvain", nextStep);
                }

                /**
                 * Notify to the user an error.
                 * 
                 * @param msg
                 *          The message to show to the user.
                 */
                public void sendError(String msg) {
                    notify(context.getString(R.string.error) + " :" + msg);
                    mProgress.cancel();
                }

                /**
                 * Notify to the user a message.
                 * 
                 * @param msg
                 *          The message to show to the user.
                 */
                public void notify(final String msg) {
                    mHandler.post(new Runnable() {
                        public void run() {
                            context.notify(msg);
                        }
                    });
                }

                public void run() {
                    progress(0, context.getString(R.string.connection_ucl));
                    UCLouvain uclouvain = new UCLouvain(username, password);

                    progress(20, context.getString(R.string.fetch_info));
                    ArrayList<Offer> offers = uclouvain.getOffers(academicYear);

                    if (offers == null || offers.isEmpty()) {
                        sendError(context.getString(R.string.error_academic_year) + academicYear);
                        return;
                    }

                    int i = 40;
                    ArrayList<Course> courses = new ArrayList<Course>();
                    for (Offer o : offers) {
                        progress(i, context.getString(R.string.get_courses) + o.getOfferName());
                        ArrayList<Course> offerCourses = uclouvain.getCourses(o);
                        if (offerCourses != null && !offerCourses.isEmpty()) {
                            courses.addAll(offerCourses);
                        } else {
                            Log.e("CoursListEditActivity", "Error : No course for offer [" + o.getOfferCode()
                                    + "] " + o.getOfferName());
                        }
                        i += (int) (30. / offers.size());
                    }

                    if (courses.isEmpty()) {
                        sendError(context.getString(R.string.courses_empty));
                        return;
                    }

                    // Remove old courses.
                    progress(70, context.getString(R.string.cleaning_db));
                    LLNCampus.getDatabase().delete("Courses", "", null);
                    LLNCampus.getDatabase().delete("Horaire", "", null);

                    // Add new data.
                    i = 80;
                    for (Course c : courses) {
                        progress(i, context.getString(R.string.add_courses_db));
                        ContentValues cv = new ContentValues();
                        cv.put("CODE", c.getCourseCode());
                        cv.put("NAME", c.getCoursName());

                        LLNCampus.getDatabase().insert("Courses", cv);
                        i += (int) (20. / courses.size());
                    }

                    progress(100, context.getString(R.string.end));
                    mProgress.cancel();
                    mHandler.post(end);
                }
            }).start();
        }
    });
}

From source file:com.nextgis.mobile.map.LocalGeoJsonLayer.java

/**
 * Create a LocalGeoJsonLayer from the GeoJson data submitted by uri.
 *//*from www.  j a v  a2 s .  c  o m*/
protected static void create(final MapBase map, String layerName, Uri uri) {

    String sErr = map.getContext().getString(R.string.error_occurred);
    ProgressDialog progressDialog = new ProgressDialog(map.getContext());

    try {
        InputStream inputStream = map.getContext().getContentResolver().openInputStream(uri);
        if (inputStream != null) {

            progressDialog.setMessage(map.getContext().getString(R.string.message_loading_progress));
            progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressDialog.setCancelable(true);
            progressDialog.show();

            int nSize = inputStream.available();
            int nIncrement = 0;
            progressDialog.setMax(nSize);

            //read all geojson
            BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));

            StringBuilder responseStrBuilder = new StringBuilder();

            String inputStr;
            while ((inputStr = streamReader.readLine()) != null) {
                nIncrement += inputStr.length();
                progressDialog.setProgress(nIncrement);
                responseStrBuilder.append(inputStr);
            }

            progressDialog.setMessage(map.getContext().getString(R.string.message_opening_progress));

            JSONObject geoJSONObject = new JSONObject(responseStrBuilder.toString());

            if (!geoJSONObject.has(GEOJSON_TYPE)) {
                sErr += ": " + map.getContext().getString(R.string.error_geojson_unsupported);
                Toast.makeText(map.getContext(), sErr, Toast.LENGTH_SHORT).show();
                progressDialog.hide();
                return;
            }

            //check crs
            boolean isWGS84 = true; //if no crs tag - WGS84 CRS

            if (geoJSONObject.has(GEOJSON_CRS)) {
                JSONObject crsJSONObject = geoJSONObject.getJSONObject(GEOJSON_CRS);

                //the link is unsupported yet.
                if (!crsJSONObject.getString(GEOJSON_TYPE).equals(GEOJSON_NAME)) {
                    sErr += ": " + map.getContext().getString(R.string.error_geojson_crs_unsupported);
                    Toast.makeText(map.getContext(), sErr, Toast.LENGTH_SHORT).show();
                    progressDialog.hide();
                    return;
                }

                JSONObject crsPropertiesJSONObject = crsJSONObject.getJSONObject(GEOJSON_PROPERTIES);
                String crsName = crsPropertiesJSONObject.getString(GEOJSON_NAME);

                if (crsName.equals("urn:ogc:def:crs:OGC:1.3:CRS84")) { // WGS84
                    isWGS84 = true;
                } else if (crsName.equals("urn:ogc:def:crs:EPSG::3857")) { //Web Mercator
                    isWGS84 = false;
                } else {
                    sErr += ": " + map.getContext().getString(R.string.error_geojson_crs_unsupported);
                    Toast.makeText(map.getContext(), sErr, Toast.LENGTH_SHORT).show();
                    progressDialog.hide();
                    return;
                }
            }

            //load contents to memory and reproject if needed
            JSONArray geoJSONFeatures = geoJSONObject.getJSONArray(GEOJSON_TYPE_FEATURES);

            if (0 == geoJSONFeatures.length()) {
                sErr += ": " + map.getContext().getString(R.string.error_geojson_crs_unsupported);
                Toast.makeText(map.getContext(), sErr, Toast.LENGTH_SHORT).show();
                progressDialog.hide();
                return;
            }

            List<Feature> features = geoJSONFeaturesToFeatures(geoJSONFeatures, isWGS84, map.getContext(),
                    progressDialog);

            create(map, layerName, features);
        }

    } catch (UnsupportedEncodingException e) {
        Log.d(TAG, "Exception: " + e.getLocalizedMessage());
        sErr += ": " + e.getLocalizedMessage();
    } catch (FileNotFoundException e) {
        Log.d(TAG, "Exception: " + e.getLocalizedMessage());
        sErr += ": " + e.getLocalizedMessage();
    } catch (IOException e) {
        Log.d(TAG, "Exception: " + e.getLocalizedMessage());
        sErr += ": " + e.getLocalizedMessage();
    } catch (JSONException e) {
        Log.d(TAG, "Exception: " + e.getLocalizedMessage());
        sErr += ": " + e.getLocalizedMessage();
    }

    progressDialog.hide();
    //if we here something wrong occurred
    Toast.makeText(map.getContext(), sErr, Toast.LENGTH_SHORT).show();
}