Example usage for android.os Handler.Callback handleMessage

List of usage examples for android.os Handler.Callback handleMessage

Introduction

In this page you can find the example usage for android.os Handler.Callback handleMessage.

Prototype

public void handleMessage(Message msg) 

Source Link

Document

Subclasses must implement this to receive messages.

Usage

From source file:Main.java

/**
 * This method will cause a view to fade out after which it fires a callback where operations can be performed then it will fade back in
 * @param view the view to fade/*from   w w w .ja va  2 s  .c  om*/
 * @param callback the callback to execute after the view has faded out
 */
public static void fadeOutIn(final View view, final Handler.Callback callback) {
    final Animation in = new AlphaAnimation(0.0f, 1.0f);
    in.setDuration(ANIMATION_SPEED);
    final Animation out = new AlphaAnimation(1.0f, 0.0f);
    out.setDuration(ANIMATION_SPEED);
    out.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            callback.handleMessage(null);
            view.startAnimation(in);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }
    });
    view.startAnimation(out);
}

From source file:org.glanceable.tweet.TweetGridPagerAdapter.java

@Override
public void onSave(long tweetId, String[] urls, final Handler.Callback callback) {
    PutDataMapRequest dataMap = PutDataMapRequest.createWithAutoAppendedId(Constants.PATH_POCKET_SAVE);
    dataMap.getDataMap().putStringArray("urls", urls);
    dataMap.getDataMap().putLong("id", tweetId);

    PutDataRequest request = dataMap.asPutDataRequest();
    PendingResult<DataApi.DataItemResult> pendingResult = Wearable.DataApi.putDataItem(mGoogleApiClient,
            request);//from  ww w  .  j a v a2 s .co m
    pendingResult.setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
        @Override
        public void onResult(DataApi.DataItemResult dataItemResult) {
            Log.i(TAG, "data send status:" + dataItemResult.getStatus());
            callback.handleMessage(null);
        }
    });
}

From source file:org.akvo.caddisfly.sensor.colorimetry.liquid.CalibrateListActivity.java

/**
 * Load the calibrated swatches from the calibration text file
 *
 * @param callback callback to be initiated once the loading is complete
 *//*from w w  w.  j a va2  s .  c o  m*/
private void loadCalibration(@NonNull final Context context, @NonNull final Handler.Callback callback) {
    try {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(R.string.loadCalibration);

        final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(context, R.layout.row_text);

        final File path = FileHelper.getFilesDir(FileHelper.FileType.CALIBRATION,
                CaddisflyApp.getApp().getCurrentTestInfo().getId());

        File[] listFilesTemp = null;
        if (path.exists() && path.isDirectory()) {
            listFilesTemp = path.listFiles();
        }

        final File[] listFiles = listFilesTemp;
        if (listFiles != null && listFiles.length > 0) {
            Arrays.sort(listFiles);

            for (File listFile : listFiles) {
                arrayAdapter.add(listFile.getName());
            }

            builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(@NonNull DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });

            builder.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String fileName = listFiles[which].getName();
                    try {
                        final List<Swatch> swatchList = SwatchHelper.loadCalibrationFromFile(getBaseContext(),
                                fileName);

                        (new AsyncTask<Void, Void, Void>() {
                            @Nullable
                            @Override
                            protected Void doInBackground(Void... params) {
                                SwatchHelper.saveCalibratedSwatches(context, swatchList);
                                return null;
                            }

                            @Override
                            protected void onPostExecute(Void result) {
                                super.onPostExecute(result);
                                callback.handleMessage(null);
                            }
                        }).execute();

                    } catch (Exception ex) {
                        AlertUtil.showError(context, R.string.error, getString(R.string.errorLoadingFile), null,
                                R.string.ok, new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(@NonNull DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                }, null, null);
                    }
                }

            });

            final AlertDialog alertDialog = builder.create();
            alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
                @Override
                public void onShow(DialogInterface dialogInterface) {
                    final ListView listView = alertDialog.getListView();
                    listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                        @Override
                        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                            final int position = i;

                            AlertUtil.askQuestion(context, R.string.delete, R.string.deleteConfirm,
                                    R.string.delete, R.string.cancel, true,
                                    new DialogInterface.OnClickListener() {
                                        @SuppressWarnings("unchecked")
                                        @Override
                                        public void onClick(DialogInterface dialogInterface, int i) {
                                            String fileName = listFiles[position].getName();
                                            FileUtil.deleteFile(path, fileName);
                                            ArrayAdapter listAdapter = (ArrayAdapter) listView.getAdapter();
                                            listAdapter.remove(listAdapter.getItem(position));
                                            alertDialog.dismiss();
                                            Toast.makeText(context, R.string.deleted, Toast.LENGTH_SHORT)
                                                    .show();
                                        }
                                    }, null);
                            return true;
                        }
                    });

                }
            });
            alertDialog.show();
        } else {
            AlertUtil.showMessage(context, R.string.notFound, R.string.loadFilesNotAvailable);
        }
    } catch (ActivityNotFoundException ignored) {
    }

    callback.handleMessage(null);
}

From source file:org.akvo.caddisfly.ui.activity.MainActivity.java

@Override
public void onLoadCalibration(final Handler.Callback callback) {
    final Context context = this;
    final MainApp mainApp = (MainApp) this.getApplicationContext();

    try {/*from  w  ww .  j a  v a2  s.c  o m*/
        AlertDialog.Builder builderSingle = new AlertDialog.Builder(context);
        builderSingle.setIcon(R.drawable.ic_launcher);
        builderSingle.setTitle(R.string.loadCalibration);

        final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context,
                android.R.layout.select_dialog_singlechoice);

        File external = Environment.getExternalStorageDirectory();
        final String folderName = Config.CALIBRATE_FOLDER_NAME;
        String path = external.getPath() + folderName;
        File folder = new File(path);
        if (folder.exists()) {
            final File[] listFiles = folder.listFiles();
            for (File listFile : listFiles) {
                arrayAdapter.add(listFile.getName());
            }

            builderSingle.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });

            builderSingle.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String fileName = listFiles[which].getName();
                    final ArrayList<Integer> swatchList = new ArrayList<Integer>();

                    final ArrayList<String> rgbList = FileUtils.loadFromFile(fileName);
                    if (rgbList != null) {

                        for (String rgb : rgbList) {
                            swatchList.add(ColorUtils.getColorFromRgb(rgb));
                        }
                        (new AsyncTask<Void, Void, Void>() {
                            @Override
                            protected Void doInBackground(Void... params) {
                                mainApp.saveCalibratedSwatches(mainApp.currentTestInfo.getCode(), swatchList);

                                mainApp.setSwatches(mainApp.currentTestInfo.getCode());

                                SharedPreferences sharedPreferences = PreferenceManager
                                        .getDefaultSharedPreferences(context);
                                SharedPreferences.Editor editor = sharedPreferences.edit();

                                for (int i = 0; i < mainApp.rangeIntervals.size(); i++) {
                                    int index = i * mainApp.rangeIncrementStep;

                                    ColorUtils.autoGenerateColors(index, mainApp.currentTestInfo.getCode(),
                                            mainApp.colorList, mainApp.rangeIncrementStep, editor, 0);
                                }
                                editor.apply();
                                return null;
                            }

                            @Override
                            protected void onPostExecute(Void result) {
                                super.onPostExecute(result);
                                callback.handleMessage(null);
                            }
                        }).execute();

                    }
                }
            });

            final AlertDialog alert = builderSingle.create();
            alert.setOnShowListener(new DialogInterface.OnShowListener() {
                @Override
                public void onShow(DialogInterface dialogInterface) {
                    final ListView listView = alert.getListView();
                    listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                        @Override
                        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                            final int position = i;

                            AlertUtils.askQuestion(context, R.string.delete, R.string.selectedWillBeDeleted,
                                    new DialogInterface.OnClickListener() {
                                        @SuppressWarnings("unchecked")
                                        @Override
                                        public void onClick(DialogInterface dialogInterface, int i) {
                                            String fileName = listFiles[position].getName();
                                            FileUtils.deleteFile(folderName, fileName);
                                            ArrayAdapter listAdapter = (ArrayAdapter) listView.getAdapter();
                                            listAdapter.remove(listAdapter.getItem(position));
                                        }
                                    });
                            return true;
                        }
                    });

                }
            });
            alert.show();
        } else {
            AlertUtils.showMessage(context, R.string.notFound, R.string.noSavedCalibrations);
        }
    } catch (ActivityNotFoundException e) {
        AlertUtils.showMessage(context, R.string.error, R.string.updateRequired);
    }

    callback.handleMessage(null);
}