Search in sources :

Example 6 with Payload

use of com.ichi2.async.Connection.Payload in project Anki-Android by Ramblurr.

the class Feedback method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    Themes.applyTheme(this);
    super.onCreate(savedInstanceState);
    Resources res = getResources();
    Context context = getBaseContext();
    SharedPreferences sharedPreferences = AnkiDroidApp.getSharedPrefs(context);
    mReportErrorMode = sharedPreferences.getString("reportErrorMode", REPORT_ASK);
    mNonce = UUID.randomUUID().getMostSignificantBits();
    mFeedbackUrl = res.getString(R.string.feedback_post_url);
    mErrorUrl = res.getString(R.string.error_post_url);
    mImm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    mPostingFeedback = false;
    initAllAlertDialogs();
    getErrorFiles();
    Intent i = getIntent();
    mAllowFeedback = (i.hasExtra("request") && (i.getIntExtra("request", 0) == DeckPicker.REPORT_FEEDBACK || i.getIntExtra("request", 0) == DeckPicker.RESULT_DB_ERROR)) || mReportErrorMode.equals(REPORT_ASK);
    if (!mAllowFeedback) {
        if (mReportErrorMode.equals(REPORT_ALWAYS)) {
            // Always report
            try {
                String feedback = "Automatically sent";
                Connection.sendFeedback(mSendListener, new Payload(new Object[] { mFeedbackUrl, mErrorUrl, feedback, mErrorReports, mNonce, getApplication(), true }));
                if (mErrorReports.size() > 0) {
                    mPostingFeedback = true;
                }
                if (feedback.length() > 0) {
                    mPostingFeedback = true;
                }
            } catch (Exception e) {
                Log.e(AnkiDroidApp.TAG, e.toString());
            }
            finish();
            if (AnkiDroidApp.SDK_VERSION > 4) {
                ActivityTransitionAnimation.slide(Feedback.this, ActivityTransitionAnimation.NONE);
            }
            return;
        } else if (mReportErrorMode.equals(REPORT_NEVER)) {
            // Never report
            deleteFiles(false, false);
            finish();
            if (AnkiDroidApp.SDK_VERSION > 4) {
                ActivityTransitionAnimation.slide(Feedback.this, ActivityTransitionAnimation.NONE);
            }
            return;
        }
    }
    View mainView = getLayoutInflater().inflate(R.layout.feedback, null);
    setContentView(mainView);
    Themes.setWallpaper(mainView);
    Themes.setTextViewStyle(findViewById(R.id.tvFeedbackDisclaimer));
    Themes.setTextViewStyle(findViewById(R.id.lvFeedbackErrorList));
    Button btnSend = (Button) findViewById(R.id.btnFeedbackSend);
    Button btnKeepLatest = (Button) findViewById(R.id.btnFeedbackKeepLatest);
    Button btnClearAll = (Button) findViewById(R.id.btnFeedbackClearAll);
    mEtFeedbackText = (EditText) findViewById(R.id.etFeedbackText);
    mLvErrorList = (ListView) findViewById(R.id.lvFeedbackErrorList);
    mErrorAdapter = new SimpleAdapter(this, mErrorReports, R.layout.error_item, new String[] { "name", "state", "result" }, new int[] { R.id.error_item_text, R.id.error_item_progress, R.id.error_item_status });
    mErrorAdapter.setViewBinder(new SimpleAdapter.ViewBinder() {

        @Override
        public boolean setViewValue(View view, Object arg1, String text) {
            switch(view.getId()) {
                case R.id.error_item_progress:
                    if (text.equals(STATE_UPLOADING)) {
                        view.setVisibility(View.VISIBLE);
                    } else {
                        view.setVisibility(View.GONE);
                    }
                    return true;
                case R.id.error_item_status:
                    if (text.length() == 0) {
                        view.setVisibility(View.GONE);
                        return true;
                    } else {
                        view.setVisibility(View.VISIBLE);
                        return false;
                    }
            }
            return false;
        }
    });
    btnClearAll.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            deleteFiles(false, false);
            refreshErrorListView();
            refreshInterface();
        }
    });
    mLvErrorList.setAdapter(mErrorAdapter);
    btnSend.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (!mPostingFeedback) {
                String feedback = mEtFeedbackText.getText().toString();
                Connection.sendFeedback(mSendListener, new Payload(new Object[] { mFeedbackUrl, mErrorUrl, feedback, mErrorReports, mNonce, getApplication(), false }));
                if (mErrorReports.size() > 0) {
                    mPostingFeedback = true;
                }
                if (feedback.length() > 0) {
                    mPostingFeedback = true;
                }
                refreshInterface();
            }
        }
    });
    btnKeepLatest.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            deleteFiles(false, true);
            refreshErrorListView();
            refreshInterface();
        }
    });
    refreshInterface();
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
}
Also used : Context(android.content.Context) SharedPreferences(android.content.SharedPreferences) SimpleAdapter(android.widget.SimpleAdapter) Intent(android.content.Intent) View(android.view.View) ListView(android.widget.ListView) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Button(android.widget.Button) OnClickListener(android.view.View.OnClickListener) Payload(com.ichi2.async.Connection.Payload) Resources(android.content.res.Resources)

Example 7 with Payload

use of com.ichi2.async.Connection.Payload in project Anki-Android by Ramblurr.

the class Connection method doInBackgroundSync.

private Payload doInBackgroundSync(Payload data) {
    // for for doInBackgroundLoadDeckCounts if any
    DeckTask.waitToFinish();
    String hkey = (String) data.data[0];
    boolean media = (Boolean) data.data[1];
    String conflictResolution = (String) data.data[2];
    int mediaUsn = (Integer) data.data[3];
    boolean colCorruptFullSync = false;
    Collection col = AnkiDroidApp.getCol();
    if (!AnkiDroidApp.colIsOpen()) {
        if (conflictResolution != null && conflictResolution.equals("download")) {
            colCorruptFullSync = true;
        } else {
            data.success = false;
            data.result = new Object[] { "genericError" };
            return data;
        }
    }
    String path = AnkiDroidApp.getCollectionPath();
    BasicHttpSyncer server = new RemoteServer(this, hkey);
    Syncer client = new Syncer(col, server);
    // run sync and check state
    boolean noChanges = false;
    if (conflictResolution == null) {
        Log.i(AnkiDroidApp.TAG, "Sync - starting sync");
        publishProgress(R.string.sync_prepare_syncing);
        Object[] ret = client.sync(this);
        data.message = client.getSyncMsg();
        mediaUsn = client.getmMediaUsn();
        if (ret == null) {
            data.success = false;
            data.result = new Object[] { "genericError" };
            return data;
        }
        String retCode = (String) ret[0];
        if (!retCode.equals("noChanges") && !retCode.equals("success")) {
            data.success = false;
            data.result = ret;
            // note mediaUSN for later
            data.data = new Object[] { mediaUsn };
            return data;
        }
        // save and note success state
        if (retCode.equals("noChanges")) {
            // publishProgress(R.string.sync_no_changes_message);
            noChanges = true;
        } else {
        // publishProgress(R.string.sync_database_success);
        }
    } else {
        try {
            server = new FullSyncer(col, hkey, this);
            if (conflictResolution.equals("upload")) {
                Log.i(AnkiDroidApp.TAG, "Sync - fullsync - upload collection");
                publishProgress(R.string.sync_preparing_full_sync_message);
                Object[] ret = server.upload();
                if (ret == null) {
                    data.success = false;
                    data.result = new Object[] { "genericError" };
                    AnkiDroidApp.openCollection(path);
                    return data;
                }
                if (!((String) ret[0]).equals(BasicHttpSyncer.ANKIWEB_STATUS_OK)) {
                    data.success = false;
                    data.result = ret;
                    AnkiDroidApp.openCollection(path);
                    return data;
                }
            } else if (conflictResolution.equals("download")) {
                Log.i(AnkiDroidApp.TAG, "Sync - fullsync - download collection");
                publishProgress(R.string.sync_downloading_message);
                Object[] ret = server.download();
                if (ret == null) {
                    data.success = false;
                    data.result = new Object[] { "genericError" };
                    AnkiDroidApp.openCollection(path);
                    return data;
                }
                if (!((String) ret[0]).equals("success")) {
                    data.success = false;
                    data.result = ret;
                    if (!colCorruptFullSync) {
                        AnkiDroidApp.openCollection(path);
                    }
                    return data;
                }
            }
            col = AnkiDroidApp.openCollection(path);
        } catch (OutOfMemoryError e) {
            AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundSync-fullSync");
            data.success = false;
            data.result = new Object[] { "OutOfMemoryError" };
            data.data = new Object[] { mediaUsn };
            return data;
        } catch (RuntimeException e) {
            AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundSync-fullSync");
            data.success = false;
            data.result = new Object[] { "IOException" };
            data.data = new Object[] { mediaUsn };
            return data;
        }
    }
    // clear undo to avoid non syncing orphans (because undo resets usn too 
    if (!noChanges) {
        col.clearUndo();
    }
    // then move on to media sync
    boolean noMediaChanges = false;
    String mediaError = null;
    if (media) {
        server = new RemoteMediaServer(hkey, this);
        MediaSyncer mediaClient = new MediaSyncer(col, (RemoteMediaServer) server);
        String ret;
        try {
            ret = mediaClient.sync(mediaUsn, this);
            if (ret == null) {
                mediaError = AnkiDroidApp.getAppResources().getString(R.string.sync_media_error);
            } else {
                if (ret.equals("noChanges")) {
                    publishProgress(R.string.sync_media_no_changes);
                    noMediaChanges = true;
                }
                if (ret.equals("sanityFailed")) {
                    mediaError = AnkiDroidApp.getAppResources().getString(R.string.sync_media_sanity_failed);
                } else {
                    publishProgress(R.string.sync_media_success);
                }
            }
        } catch (RuntimeException e) {
            AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundSync-mediaSync");
            mediaError = e.getLocalizedMessage();
        }
    }
    if (noChanges && noMediaChanges) {
        data.success = false;
        data.result = new Object[] { "noChanges" };
        return data;
    } else {
        data.success = true;
        TreeSet<Object[]> decks = col.getSched().deckDueTree();
        int[] counts = new int[] { 0, 0, 0 };
        for (Object[] deck : decks) {
            if (((String[]) deck[0]).length == 1) {
                counts[0] += (Integer) deck[2];
                counts[1] += (Integer) deck[3];
                counts[2] += (Integer) deck[4];
            }
        }
        Object[] dc = col.getSched().deckCounts();
        data.result = dc[0];
        data.data = new Object[] { conflictResolution, col, dc[1], dc[2], mediaError };
        return data;
    }
}
Also used : FullSyncer(com.ichi2.libanki.sync.FullSyncer) OutOfMemoryError(java.lang.OutOfMemoryError) BasicHttpSyncer(com.ichi2.libanki.sync.BasicHttpSyncer) FullSyncer(com.ichi2.libanki.sync.FullSyncer) MediaSyncer(com.ichi2.libanki.sync.MediaSyncer) BasicHttpSyncer(com.ichi2.libanki.sync.BasicHttpSyncer) Syncer(com.ichi2.libanki.sync.Syncer) Collection(com.ichi2.libanki.Collection) MediaSyncer(com.ichi2.libanki.sync.MediaSyncer) JSONObject(org.json.JSONObject) RemoteServer(com.ichi2.libanki.sync.RemoteServer) RemoteMediaServer(com.ichi2.libanki.sync.RemoteMediaServer)

Example 8 with Payload

use of com.ichi2.async.Connection.Payload in project Anki-Android by Ramblurr.

the class Connection method doInBackgroundRegister.

private Payload doInBackgroundRegister(Payload data) {
    String username = (String) data.data[0];
    String password = (String) data.data[1];
    BasicHttpSyncer server = new RemoteServer(this, null);
    HttpResponse ret = server.register(username, password);
    String hostkey = null;
    boolean valid = false;
    String status = null;
    if (ret != null) {
        data.returnType = ret.getStatusLine().getStatusCode();
        if (data.returnType == 200) {
            try {
                JSONObject jo = (new JSONObject(server.stream2String(ret.getEntity().getContent())));
                status = jo.getString("status");
                if (status.equals("ok")) {
                    hostkey = jo.getString("hkey");
                    valid = (hostkey != null) && (hostkey.length() > 0);
                }
            } catch (JSONException e) {
            } catch (IllegalStateException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
    if (valid) {
        data.success = true;
        data.data = new String[] { username, hostkey };
    } else {
        data.success = false;
        data.data = new String[] { status != null ? status : AnkiDroidApp.getAppResources().getString(R.string.connection_error_message) };
    }
    return data;
}
Also used : BasicHttpSyncer(com.ichi2.libanki.sync.BasicHttpSyncer) JSONObject(org.json.JSONObject) HttpResponse(org.apache.http.HttpResponse) JSONException(org.json.JSONException) IOException(java.io.IOException) RemoteServer(com.ichi2.libanki.sync.RemoteServer)

Aggregations

IOException (java.io.IOException)6 JSONObject (org.json.JSONObject)5 BasicHttpSyncer (com.ichi2.libanki.sync.BasicHttpSyncer)4 HttpResponse (org.apache.http.HttpResponse)4 Payload (com.ichi2.async.Connection.Payload)3 RemoteServer (com.ichi2.libanki.sync.RemoteServer)3 File (java.io.File)3 JSONException (org.json.JSONException)3 Collection (com.ichi2.libanki.Collection)2 BufferedInputStream (java.io.BufferedInputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 FileOutputStream (java.io.FileOutputStream)2 InputStream (java.io.InputStream)2 ClientProtocolException (org.apache.http.client.ClientProtocolException)2 Context (android.content.Context)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 Resources (android.content.res.Resources)1