Search in sources :

Example 1 with DialogAction

use of com.afollestad.materialdialogs.DialogAction in project material-dialogs by afollestad.

the class ColorChooserDialog method onCreateDialog.

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    if (getArguments() == null || !getArguments().containsKey("builder")) {
        throw new IllegalStateException("ColorChooserDialog should be created using its Builder interface.");
    }
    generateColors();
    int preselectColor;
    boolean foundPreselectColor = false;
    if (savedInstanceState != null) {
        foundPreselectColor = !savedInstanceState.getBoolean("in_custom", false);
        preselectColor = getSelectedColor();
    } else {
        if (getBuilder().setPreselectionColor) {
            preselectColor = getBuilder().preselectColor;
            if (preselectColor != 0) {
                for (int topIndex = 0; topIndex < colorsTop.length; topIndex++) {
                    if (colorsTop[topIndex] == preselectColor) {
                        foundPreselectColor = true;
                        topIndex(topIndex);
                        if (getBuilder().accentMode) {
                            subIndex(2);
                        } else if (colorsSub != null) {
                            findSubIndexForColor(topIndex, preselectColor);
                        } else {
                            subIndex(5);
                        }
                        break;
                    }
                    if (colorsSub != null) {
                        for (int subIndex = 0; subIndex < colorsSub[topIndex].length; subIndex++) {
                            if (colorsSub[topIndex][subIndex] == preselectColor) {
                                foundPreselectColor = true;
                                topIndex(topIndex);
                                subIndex(subIndex);
                                break;
                            }
                        }
                        if (foundPreselectColor) {
                            break;
                        }
                    }
                }
            }
        } else {
            preselectColor = Color.BLACK;
            foundPreselectColor = true;
        }
    }
    circleSize = getResources().getDimensionPixelSize(R.dimen.md_colorchooser_circlesize);
    final Builder builder = getBuilder();
    MaterialDialog.Builder bd = new MaterialDialog.Builder(getActivity()).title(getTitle()).autoDismiss(false).customView(R.layout.md_dialog_colorchooser, false).negativeText(builder.cancelBtn).positiveText(builder.doneBtn).neutralText(builder.allowUserCustom ? builder.customBtn : 0).onPositive(new MaterialDialog.SingleButtonCallback() {

        @Override
        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
            callback.onColorSelection(ColorChooserDialog.this, getSelectedColor());
            dismiss();
        }
    }).onNegative(new MaterialDialog.SingleButtonCallback() {

        @Override
        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
            if (isInSub()) {
                dialog.setActionButton(DialogAction.NEGATIVE, getBuilder().cancelBtn);
                isInSub(false);
                // Do this to avoid ArrayIndexOutOfBoundsException
                subIndex(-1);
                invalidate();
            } else {
                dialog.cancel();
            }
        }
    }).onNeutral(new MaterialDialog.SingleButtonCallback() {

        @Override
        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
            toggleCustom(dialog);
        }
    }).showListener(new DialogInterface.OnShowListener() {

        @Override
        public void onShow(DialogInterface dialog) {
            invalidateDynamicButtonColors();
        }
    });
    if (builder.theme != null) {
        bd.theme(builder.theme);
    }
    final MaterialDialog dialog = bd.build();
    final View v = dialog.getCustomView();
    grid = (GridView) v.findViewById(R.id.md_grid);
    if (builder.allowUserCustom) {
        selectedCustomColor = preselectColor;
        colorChooserCustomFrame = v.findViewById(R.id.md_colorChooserCustomFrame);
        customColorHex = (EditText) v.findViewById(R.id.md_hexInput);
        customColorIndicator = v.findViewById(R.id.md_colorIndicator);
        customSeekA = (SeekBar) v.findViewById(R.id.md_colorA);
        customSeekAValue = (TextView) v.findViewById(R.id.md_colorAValue);
        customSeekR = (SeekBar) v.findViewById(R.id.md_colorR);
        customSeekRValue = (TextView) v.findViewById(R.id.md_colorRValue);
        customSeekG = (SeekBar) v.findViewById(R.id.md_colorG);
        customSeekGValue = (TextView) v.findViewById(R.id.md_colorGValue);
        customSeekB = (SeekBar) v.findViewById(R.id.md_colorB);
        customSeekBValue = (TextView) v.findViewById(R.id.md_colorBValue);
        if (!builder.allowUserCustomAlpha) {
            v.findViewById(R.id.md_colorALabel).setVisibility(View.GONE);
            customSeekA.setVisibility(View.GONE);
            customSeekAValue.setVisibility(View.GONE);
            customColorHex.setHint("2196F3");
            customColorHex.setFilters(new InputFilter[] { new InputFilter.LengthFilter(6) });
        } else {
            customColorHex.setHint("FF2196F3");
            customColorHex.setFilters(new InputFilter[] { new InputFilter.LengthFilter(8) });
        }
        if (!foundPreselectColor) {
            // If color wasn't found in the preset colors, it must be custom
            toggleCustom(dialog);
        }
    }
    invalidate();
    return dialog;
}
Also used : InputFilter(android.text.InputFilter) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) DialogInterface(android.content.DialogInterface) GridView(android.widget.GridView) View(android.view.View) TextView(android.widget.TextView) SuppressLint(android.annotation.SuppressLint) DialogAction(com.afollestad.materialdialogs.DialogAction) NonNull(android.support.annotation.NonNull) NonNull(android.support.annotation.NonNull)

Example 2 with DialogAction

use of com.afollestad.materialdialogs.DialogAction in project material-dialogs by afollestad.

the class FolderChooserDialog method onCreateDialog.

@SuppressWarnings("ConstantConditions")
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        return new MaterialDialog.Builder(getActivity()).title(R.string.md_error_label).content(R.string.md_storage_perm_error).positiveText(android.R.string.ok).build();
    }
    if (getArguments() == null || !getArguments().containsKey("builder")) {
        throw new IllegalStateException("You must create a FolderChooserDialog using the Builder.");
    }
    if (!getArguments().containsKey("current_path")) {
        getArguments().putString("current_path", getBuilder().initialPath);
    }
    parentFolder = new File(getArguments().getString("current_path"));
    checkIfCanGoUp();
    parentContents = listFiles();
    MaterialDialog.Builder builder = new MaterialDialog.Builder(getActivity()).title(parentFolder.getAbsolutePath()).items((CharSequence[]) getContentsArray()).itemsCallback(this).onPositive(new MaterialDialog.SingleButtonCallback() {

        @Override
        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
            dialog.dismiss();
            callback.onFolderSelection(FolderChooserDialog.this, parentFolder);
        }
    }).onNegative(new MaterialDialog.SingleButtonCallback() {

        @Override
        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
            dialog.dismiss();
        }
    }).autoDismiss(false).positiveText(getBuilder().chooseButton).negativeText(getBuilder().cancelButton);
    if (getBuilder().allowNewFolder) {
        builder.neutralText(getBuilder().newFolderButton);
        builder.onNeutral(new MaterialDialog.SingleButtonCallback() {

            @Override
            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                createNewFolder();
            }
        });
    }
    if ("/".equals(getBuilder().initialPath)) {
        canGoUp = false;
    }
    return builder.build();
}
Also used : MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) DialogAction(com.afollestad.materialdialogs.DialogAction) NonNull(android.support.annotation.NonNull) File(java.io.File) NonNull(android.support.annotation.NonNull)

Example 3 with DialogAction

use of com.afollestad.materialdialogs.DialogAction in project material-dialogs by afollestad.

the class MaterialDialogPreference method showDialog.

@Override
protected void showDialog(Bundle state) {
    MaterialDialog.Builder builder = new MaterialDialog.Builder(context).title(getDialogTitle()).icon(getDialogIcon()).dismissListener(this).onAny(new MaterialDialog.SingleButtonCallback() {

        @Override
        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
            switch(which) {
                default:
                    MaterialDialogPreference.this.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                    break;
                case NEUTRAL:
                    MaterialDialogPreference.this.onClick(dialog, DialogInterface.BUTTON_NEUTRAL);
                    break;
                case NEGATIVE:
                    MaterialDialogPreference.this.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                    break;
            }
        }
    }).positiveText(getPositiveButtonText()).negativeText(getNegativeButtonText()).autoDismiss(// immediately close the dialog after selection
    true);
    final View contentView = onCreateDialogView();
    if (contentView != null) {
        onBindDialogView(contentView);
        builder.customView(contentView, false);
    } else {
        builder.content(getDialogMessage());
    }
    PrefUtil.registerOnActivityDestroyListener(this, this);
    dialog = builder.build();
    if (state != null) {
        dialog.onRestoreInstanceState(state);
    }
    dialog.show();
}
Also used : MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) DialogAction(com.afollestad.materialdialogs.DialogAction) NonNull(android.support.annotation.NonNull) View(android.view.View)

Example 4 with DialogAction

use of com.afollestad.materialdialogs.DialogAction in project material-camera by afollestad.

the class BaseCameraFragment method onClick.

@Override
public void onClick(View view) {
    final int id = view.getId();
    if (id == R.id.facing) {
        mInterface.toggleCameraPosition();
        setImageRes(mButtonFacing, mInterface.getCurrentCameraPosition() == BaseCaptureActivity.CAMERA_POSITION_BACK ? mInterface.iconFrontCamera() : mInterface.iconRearCamera());
        closeCamera();
        openCamera();
        setupFlashMode();
    } else if (id == R.id.video) {
        if (mIsRecording) {
            stopRecordingVideo(false);
            mIsRecording = false;
        } else {
            if (getArguments().getBoolean(CameraIntentKey.SHOW_PORTRAIT_WARNING, true) && Degrees.isPortrait(getActivity())) {
                new MaterialDialog.Builder(getActivity()).title(R.string.mcam_portrait).content(R.string.mcam_portrait_warning).positiveText(R.string.mcam_yes).negativeText(android.R.string.cancel).onPositive(new MaterialDialog.SingleButtonCallback() {

                    @Override
                    public void onClick(@NonNull MaterialDialog materialDialog, @NonNull DialogAction dialogAction) {
                        mIsRecording = startRecordingVideo();
                    }
                }).show();
            } else {
                mIsRecording = startRecordingVideo();
            }
        }
    } else if (id == R.id.stillshot) {
        takeStillshot();
    } else if (id == R.id.flash) {
        invalidateFlash(true);
    }
}
Also used : MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) DialogAction(com.afollestad.materialdialogs.DialogAction) SuppressLint(android.annotation.SuppressLint)

Example 5 with DialogAction

use of com.afollestad.materialdialogs.DialogAction in project CoCoin by Nightonke.

the class AccountBookTodayViewActivity method sync.

private void sync() {
    if (!SettingManager.getInstance().getLoggenOn()) {
        CoCoinUtil.showToast(mContext, R.string.login_tip);
    } else {
        syncSuccessNumber = 0;
        syncFailedNumber = 0;
        syncQueryDialog = new MaterialDialog.Builder(this).title(R.string.sync_querying_title).content(R.string.sync_querying_content).negativeText(R.string.cancel).progress(true, 0).onAny(new MaterialDialog.SingleButtonCallback() {

            @Override
            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                if (which == DialogAction.NEGATIVE) {
                }
            }
        }).show();
        final User user = BmobUser.getCurrentUser(CoCoinApplication.getAppContext(), User.class);
        final MyQuery myQuery = new MyQuery();
        myQuery.setTask(++TaskManager.QUERY_UPDATE_TASK);
        myQuery.query = new BmobQuery<>();
        myQuery.query.addWhereEqualTo("userId", user.getObjectId());
        myQuery.query.setLimit(1);
        myQuery.query.findObjects(CoCoinApplication.getAppContext(), new FindListener<UploadInfo>() {

            @Override
            public void onSuccess(List<UploadInfo> object) {
                if (myQuery.getTask() != TaskManager.QUERY_UPDATE_TASK)
                    return;
                else {
                    syncQueryDialog.dismiss();
                    cloudRecordNumber = 0;
                    Calendar cal = null;
                    if (object.size() == 0) {
                    } else {
                        cloudRecordNumber = object.get(0).getRecordNumber();
                        cloudOldDatabaseUrl = object.get(0).getDatabaseUrl();
                        cloudOldDatabaseFileName = object.get(0).getFileName();
                        uploadObjectId = object.get(0).getObjectId();
                        cal = Calendar.getInstance();
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        try {
                            cal.setTime(sdf.parse(object.get(0).getUpdatedAt()));
                        } catch (ParseException p) {
                        }
                    }
                    String content = CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.sync_info_cloud_record_0) + cloudRecordNumber + CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.sync_info_cloud_record_1) + (cal == null ? CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.sync_info_cloud_time_2) : CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.sync_info_cloud_time_0) + CoCoinUtil.GetCalendarString(CoCoinApplication.getAppContext(), cal) + CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.sync_info_cloud_time_1)) + CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.sync_info_mobile_record_0) + RecordManager.getInstance(CoCoinApplication.getAppContext()).RECORDS.size() + CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.sync_info_mobile_record_1) + (SettingManager.getInstance().getRecentlySyncTime() == null ? CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.sync_info_mobile_time_2) : CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.sync_info_mobile_time_0) + CoCoinUtil.GetCalendarString(CoCoinApplication.getAppContext(), SettingManager.getInstance().getRecentlySyncTime()) + CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.sync_info_mobile_time_1)) + CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.sync_choose_content);
                    syncChooseDialog = new MaterialDialog.Builder(mContext).title(R.string.sync_choose_title).content(content).positiveText(R.string.sync_to_cloud).negativeText(R.string.sync_to_mobile).neutralText(R.string.cancel).onAny(new MaterialDialog.SingleButtonCallback() {

                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                            syncChooseDialog.dismiss();
                            if (which == DialogAction.POSITIVE) {
                                // sync to cloud
                                String subContent = "";
                                if (RecordManager.getInstance(CoCoinApplication.getAppContext()).RECORDS.size() == 0) {
                                    subContent = CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.mobile_record_empty);
                                    new MaterialDialog.Builder(mContext).title(R.string.sync).content(subContent).positiveText(R.string.ok_1).show();
                                    return;
                                } else {
                                    subContent = CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.sure_to_cloud_0) + RecordManager.getInstance(CoCoinApplication.getAppContext()).RECORDS.size() + CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.sure_to_cloud_1);
                                }
                                new MaterialDialog.Builder(mContext).title(R.string.sync).content(subContent).positiveText(R.string.ok_1).negativeText(R.string.cancel).onAny(new MaterialDialog.SingleButtonCallback() {

                                    @Override
                                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                        if (which == DialogAction.POSITIVE) {
                                            syncProgressDialog = new MaterialDialog.Builder(mContext).title(R.string.syncing).content(CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.uploading_0) + "1" + CoCoinUtil.GetString(mContext, R.string.uploading_1)).progress(false, RecordManager.getInstance(CoCoinApplication.getAppContext()).RECORDS.size(), true).cancelable(false).show();
                                            final String databasePath = CoCoinUtil.GetRecordDatabasePath(CoCoinApplication.getAppContext());
                                            //                                                                final BmobFile bmobFile = new BmobFile(new File(databasePath));
                                            //                                                                bmobFile.uploadblock(mContext, new UploadFileListener() {
                                            //
                                            //                                                                    @Override
                                            //                                                                    public void onSuccess() {
                                            //                                                                        if (BuildConfig.DEBUG) {
                                            //                                                                            Log.d("CoCoin", "Upload successfully fileName: " + databasePath);
                                            //                                                                            Log.d("CoCoin", "Upload successfully url: " + bmobFile.getFileUrl(mContext));
                                            //                                                                        }
                                            //                                                                        // the new database is uploaded successfully
                                            //                                                                        // delete the old database(if there is)
                                            //                                                                        if (cloudOldDatabaseUrl != null) {
                                            //                                                                            deleteOldDatabaseOnCloud(cloudOldDatabaseUrl);
                                            //                                                                        }
                                            //                                                                        // update the UploadInfo record for the new url
                                            //                                                                        if (uploadObjectId == null) {
                                            //                                                                            // first time
                                            //                                                                            UploadInfo uploadInfo = new UploadInfo();
                                            //                                                                            uploadInfo.setUserId(user.getObjectId());
                                            //                                                                            uploadInfo.setRecordNumber(RecordManager.getInstance(mContext).RECORDS.size());
                                            //                                                                            uploadInfo.setDatabaseUrl(bmobFile.getFileUrl(mContext));
                                            //                                                                            uploadInfo.save(mContext, new SaveListener() {
                                            //                                                                                @Override
                                            //                                                                                public void onSuccess() {
                                            //                                                                                    // upload successfully
                                            //                                                                                    syncProgressDialog.dismiss();
                                            //                                                                                    new MaterialDialog.Builder(mContext)
                                            //                                                                                            .title(R.string.sync_completely_title)
                                            //                                                                                            .content(RecordManager.getInstance(mContext).RECORDS.size() + CoCoinUtil.GetString(mContext, R.string.uploading_fail_1))
                                            //                                                                                            .positiveText(R.string.ok_1)
                                            //                                                                                            .show();
                                            //                                                                                }
                                            //                                                                                @Override
                                            //                                                                                public void onFailure(int code, String arg0) {
                                            //                                                                                    // 添加失败
                                            //                                                                                }
                                            //                                                                            });
                                            //                                                                        } else {
                                            //                                                                            UploadInfo uploadInfo = new UploadInfo();
                                            //                                                                            uploadInfo.setUserId(user.getObjectId());
                                            //                                                                            uploadInfo.setRecordNumber(RecordManager.getInstance(mContext).RECORDS.size());
                                            //                                                                            uploadInfo.setDatabaseUrl(bmobFile.getFileUrl(mContext));
                                            //                                                                            uploadInfo.update(mContext, uploadObjectId, new UpdateListener() {
                                            //                                                                                @Override
                                            //                                                                                public void onSuccess() {
                                            //                                                                                    // upload successfully
                                            //                                                                                    syncProgressDialog.dismiss();
                                            //                                                                                    new MaterialDialog.Builder(mContext)
                                            //                                                                                            .title(R.string.sync_completely_title)
                                            //                                                                                            .content(RecordManager.getInstance(mContext).RECORDS.size() + CoCoinUtil.GetString(mContext, R.string.uploading_fail_1))
                                            //                                                                                            .positiveText(R.string.ok_1)
                                            //                                                                                            .show();
                                            //                                                                                }
                                            //                                                                                @Override
                                            //                                                                                public void onFailure(int code, String msg) {
                                            //                                                                                    // upload failed
                                            //                                                                                    Log.i("bmob","更新失败:"+msg);
                                            //                                                                                }
                                            //                                                                            });
                                            //                                                                        }
                                            //                                                                    }
                                            //
                                            //                                                                    @Override
                                            //                                                                    public void onProgress(Integer value) {
                                            //                                                                        syncProgressDialog.setProgress(value);
                                            //                                                                    }
                                            //
                                            //                                                                    @Override
                                            //                                                                    public void onFailure(int code, String msg) {
                                            //                                                                        // upload failed
                                            //                                                                        if (BuildConfig.DEBUG) Log.d("CoCoin", "Upload database failed " + code + " " + msg);
                                            //                                                                        syncProgressDialog.dismiss();
                                            //                                                                        new MaterialDialog.Builder(mContext)
                                            //                                                                                .title(R.string.sync_failed)
                                            //                                                                                .content(R.string.uploading_fail_0)
                                            //                                                                                .positiveText(R.string.ok_1)
                                            //                                                                                .show();
                                            //                                                                    }
                                            //                                                                });
                                            BmobProFile.getInstance(CoCoinApplication.getAppContext()).upload(databasePath, new UploadListener() {

                                                @Override
                                                public void onSuccess(String fileName, String url, BmobFile file) {
                                                    CoCoinUtil.deleteBmobUploadCach(CoCoinApplication.getAppContext());
                                                    if (BuildConfig.DEBUG) {
                                                        Log.d("CoCoin", "Upload successfully fileName: " + fileName);
                                                        Log.d("CoCoin", "Upload successfully url: " + url);
                                                    }
                                                    // delete the old database(if there is)
                                                    if (cloudOldDatabaseFileName != null) {
                                                        deleteOldDatabaseOnCloud(cloudOldDatabaseFileName);
                                                    }
                                                    // update the UploadInfo record for the new url
                                                    UploadInfo uploadInfo = new UploadInfo();
                                                    uploadInfo.setUserId(user.getObjectId());
                                                    uploadInfo.setRecordNumber(RecordManager.getInstance(CoCoinApplication.getAppContext()).RECORDS.size());
                                                    uploadInfo.setDatabaseUrl(file.getFileUrl(CoCoinApplication.getAppContext()));
                                                    uploadInfo.setFileName(fileName);
                                                    if (uploadObjectId == null) {
                                                        // insert
                                                        uploadInfo.save(CoCoinApplication.getAppContext(), new SaveListener() {

                                                            @Override
                                                            public void onSuccess() {
                                                                // upload successfully
                                                                syncProgressDialog.dismiss();
                                                                new MaterialDialog.Builder(mContext).title(R.string.sync_completely_title).content(RecordManager.getInstance(CoCoinApplication.getAppContext()).RECORDS.size() + CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.uploading_fail_1)).positiveText(R.string.ok_1).cancelable(false).show();
                                                            }

                                                            @Override
                                                            public void onFailure(int code, String arg0) {
                                                                uploadFailed(code, arg0);
                                                            }
                                                        });
                                                    } else {
                                                        // update
                                                        uploadInfo.update(CoCoinApplication.getAppContext(), uploadObjectId, new UpdateListener() {

                                                            @Override
                                                            public void onSuccess() {
                                                                // upload successfully
                                                                syncProgressDialog.dismiss();
                                                                new MaterialDialog.Builder(mContext).title(R.string.sync_completely_title).content(RecordManager.getInstance(CoCoinApplication.getAppContext()).RECORDS.size() + CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.uploading_fail_1)).positiveText(R.string.ok_1).cancelable(false).show();
                                                            }

                                                            @Override
                                                            public void onFailure(int code, String msg) {
                                                                uploadFailed(code, msg);
                                                            }
                                                        });
                                                    }
                                                }

                                                @Override
                                                public void onProgress(int progress) {
                                                    syncProgressDialog.setProgress((int) (progress * 1.0 / 100 * RecordManager.getInstance(CoCoinApplication.getAppContext()).RECORDS.size()));
                                                }

                                                @Override
                                                public void onError(int statuscode, String errormsg) {
                                                    // upload failed
                                                    uploadFailed(statuscode, errormsg);
                                                }
                                            });
                                        }
                                    }
                                }).show();
                            } else if (which == DialogAction.NEGATIVE) {
                                // sync to mobile
                                String subContent = "";
                                if (cloudRecordNumber == 0) {
                                    subContent = CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.cloud_record_empty);
                                    new MaterialDialog.Builder(mContext).title(R.string.sync).content(subContent).positiveText(R.string.ok_1).show();
                                    return;
                                } else {
                                    subContent = CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.sure_to_mobile_0) + cloudRecordNumber + CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.sure_to_mobile_1);
                                }
                                new MaterialDialog.Builder(mContext).title(R.string.sync).content(subContent).positiveText(R.string.ok_1).negativeText(R.string.cancel).onAny(new MaterialDialog.SingleButtonCallback() {

                                    @Override
                                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                        if (which == DialogAction.POSITIVE) {
                                            syncProgressDialog = new MaterialDialog.Builder(mContext).title(R.string.syncing).content(CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.downloading_0) + "1" + CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.downloading_1)).progress(false, cloudRecordNumber, true).cancelable(false).show();
                                            // download the database file to mobile
                                            BmobProFile.getInstance(CoCoinApplication.getAppContext()).download(cloudOldDatabaseFileName, new DownloadListener() {

                                                @Override
                                                public void onSuccess(String fullPath) {
                                                    // copy the new database to mobile
                                                    try {
                                                        Log.d("CoCoin", "Download successfully " + fullPath);
                                                        syncProgressDialog.setContent(R.string.sync_completely_content);
                                                        byte[] buffer = new byte[1024];
                                                        File file = new File(fullPath);
                                                        InputStream inputStream = new FileInputStream(file);
                                                        String outFileNameString = CoCoinUtil.GetRecordDatabasePath(CoCoinApplication.getAppContext());
                                                        OutputStream outputStream = new FileOutputStream(outFileNameString);
                                                        int length;
                                                        while ((length = inputStream.read(buffer)) > 0) {
                                                            outputStream.write(buffer, 0, length);
                                                        }
                                                        Log.d("CoCoin", "Download successfully copy completely");
                                                        outputStream.flush();
                                                        outputStream.close();
                                                        inputStream.close();
                                                        file.delete();
                                                        Log.d("CoCoin", "Download successfully delete completely");
                                                        // refresh data
                                                        RecordManager.getInstance(CoCoinApplication.getAppContext()).RECORDS.clear();
                                                        RecordManager.getInstance(CoCoinApplication.getAppContext()).RECORDS = null;
                                                        RecordManager.getInstance(CoCoinApplication.getAppContext());
                                                        todayModeAdapter.notifyDataSetChanged();
                                                        Log.d("CoCoin", "Download successfully refresh completely");
                                                        syncProgressDialog.dismiss();
                                                        new MaterialDialog.Builder(mContext).title(R.string.sync_completely_title).content(cloudRecordNumber + CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.downloading_fail_1)).positiveText(R.string.ok_1).cancelable(false).show();
                                                    } catch (IOException i) {
                                                        i.printStackTrace();
                                                    }
                                                }

                                                @Override
                                                public void onProgress(String localPath, int percent) {
                                                    syncProgressDialog.setProgress((int) (((float) percent / 100) * RecordManager.getInstance(CoCoinApplication.getAppContext()).RECORDS.size()));
                                                }

                                                @Override
                                                public void onError(int statuscode, String errormsg) {
                                                    downloadFailed(statuscode, errormsg);
                                                }
                                            });
                                        }
                                    }
                                }).show();
                            } else {
                            }
                        }
                    }).show();
                }
            }

            @Override
            public void onError(int code, String msg) {
                syncQueryDialog.dismiss();
                if (BuildConfig.DEBUG)
                    Log.d("CoCoin", "Query: " + msg);
                if (syncQueryDialog != null)
                    syncQueryDialog.dismiss();
                new MaterialDialog.Builder(mContext).title(R.string.sync_querying_fail_title).content(R.string.sync_querying_fail_content).positiveText(R.string.ok_1).show();
            }
        });
    }
}
Also used : SaveListener(cn.bmob.v3.listener.SaveListener) BmobUser(cn.bmob.v3.BmobUser) User(com.nightonke.saver.model.User) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) UploadListener(com.bmob.btp.callback.UploadListener) NonNull(android.support.annotation.NonNull) UpdateListener(cn.bmob.v3.listener.UpdateListener) BmobFile(cn.bmob.v3.datatype.BmobFile) UploadInfo(com.nightonke.saver.model.UploadInfo) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Calendar(java.util.Calendar) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) DownloadListener(com.bmob.btp.callback.DownloadListener) DialogAction(com.afollestad.materialdialogs.DialogAction) FileOutputStream(java.io.FileOutputStream) MyQuery(com.nightonke.saver.ui.MyQuery) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) BmobFile(cn.bmob.v3.datatype.BmobFile) BmobProFile(com.bmob.BmobProFile) File(java.io.File)

Aggregations

DialogAction (com.afollestad.materialdialogs.DialogAction)160 MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)160 NonNull (android.support.annotation.NonNull)93 View (android.view.View)92 TextView (android.widget.TextView)55 DialogInterface (android.content.DialogInterface)52 ImageView (android.widget.ImageView)40 Intent (android.content.Intent)38 RecyclerView (android.support.v7.widget.RecyclerView)32 SuppressLint (android.annotation.SuppressLint)23 ArrayList (java.util.ArrayList)22 ListView (android.widget.ListView)21 AdapterView (android.widget.AdapterView)20 List (java.util.List)18 File (java.io.File)16 Dialog (android.app.Dialog)13 SpannableStringBuilder (android.text.SpannableStringBuilder)12 MenuItem (android.view.MenuItem)12 PopupMenu (android.widget.PopupMenu)12 AlertDialogWrapper (com.afollestad.materialdialogs.AlertDialogWrapper)12