Search in sources :

Example 6 with Result

use of com.ichi2.anki.multimediacard.googleimagesearch.json.Result in project Anki-Android by Ramblurr.

the class MultimediaCardEditorActivity method save.

private void save() {
    NoteService.saveMedia((MultimediaEditableNote) mNote);
    NoteService.updateJsonNoteFromMultimediaNote(mNote, mEditorNote);
    TaskListener listener = new TaskListener() {

        @Override
        public void onProgressUpdate(TaskData... values) {
        // TODO Auto-generated method stub
        }

        @Override
        public void onPreExecute() {
        // TODO Auto-generated method stub
        }

        @Override
        public void onPostExecute(TaskData result) {
        // TODO Auto-generated method stub
        }
    };
    if (mAddNote) {
        DeckTask.launchDeckTask(DeckTask.TASK_TYPE_ADD_FACT, listener, new DeckTask.TaskData(mEditorNote));
    } else {
        DeckTask.launchDeckTask(DeckTask.TASK_TYPE_UPDATE_FACT, listener, new DeckTask.TaskData(mCol.getSched(), mCard, false));
    }
    setResult(Activity.RESULT_OK);
    finish();
    animateRight();
}
Also used : TaskListener(com.ichi2.async.DeckTask.TaskListener) TaskData(com.ichi2.async.DeckTask.TaskData) DeckTask(com.ichi2.async.DeckTask) TaskData(com.ichi2.async.DeckTask.TaskData)

Example 7 with Result

use of com.ichi2.anki.multimediacard.googleimagesearch.json.Result in project Anki-Android by Ramblurr.

the class Models method fieldMap.

/** "Mapping of field name -> (ord, field). */
public Map<String, Pair<Integer, JSONObject>> fieldMap(JSONObject m) {
    JSONArray ja;
    try {
        ja = m.getJSONArray("flds");
        // TreeMap<Integer, String> map = new TreeMap<Integer, String>();
        Map<String, Pair<Integer, JSONObject>> result = new HashMap<String, Pair<Integer, JSONObject>>();
        for (int i = 0; i < ja.length(); i++) {
            JSONObject f = ja.getJSONObject(i);
            result.put(f.getString("name"), new Pair<Integer, JSONObject>(f.getInt("ord"), f));
        }
        return result;
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
}
Also used : JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Pair(com.ichi2.anki.Pair)

Example 8 with Result

use of com.ichi2.anki.multimediacard.googleimagesearch.json.Result in project Anki-Android by Ramblurr.

the class DeckTask method doInBackgroundSearchCards.

private TaskData doInBackgroundSearchCards(TaskData... params) {
    Log.i(AnkiDroidApp.TAG, "doInBackgroundSearchCards");
    Collection col = (Collection) params[0].getObjArray()[0];
    HashMap<String, String> deckNames = (HashMap<String, String>) params[0].getObjArray()[1];
    String query = (String) params[0].getObjArray()[2];
    String order = (String) params[0].getObjArray()[3];
    TaskData result = new TaskData(col.findCardsForCardBrowser(query, order, deckNames));
    if (DeckTask.taskIsCancelled()) {
        return null;
    } else {
        publishProgress(result);
    }
    return new TaskData(col.cardCount(col.getDecks().allIds()));
}
Also used : HashMap(java.util.HashMap) Collection(com.ichi2.libanki.Collection)

Example 9 with Result

use of com.ichi2.anki.multimediacard.googleimagesearch.json.Result in project Anki-Android by Ramblurr.

the class Feedback method postFeedback.

/**
     * Posting feedback or error info to the server. This is called from the AsyncTask.
     * 
     * @param url The url to post the feedback to.
     * @param type The type of the info, eg Feedback.TYPE_CRASH_STACKTRACE.
     * @param feedback For feedback types this is the message. For error/crash types this is the path to the error file.
     * @param groupId A single time generated ID, so that errors/feedback send together can be grouped together.
     * @param index The index of the error in the list
     * @return A Payload file showing success, response code and response message.
     */
public static Payload postFeedback(String url, String type, String feedback, String groupId, int index, Application app) {
    Payload result = new Payload(null);
    List<NameValuePair> pairs = null;
    if (!isErrorType(type)) {
        pairs = new ArrayList<NameValuePair>();
        pairs.add(new BasicNameValuePair("type", type));
        pairs.add(new BasicNameValuePair("groupid", groupId));
        pairs.add(new BasicNameValuePair("index", "0"));
        pairs.add(new BasicNameValuePair("message", feedback));
        addTimestamp(pairs);
    } else {
        pairs = Feedback.extractPairsFromError(type, feedback, groupId, index, app);
        if (pairs == null) {
            result.success = false;
            result.result = null;
        }
    }
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);
    httpPost.addHeader("User-Agent", "AnkiDroid");
    try {
        httpPost.setEntity(new UrlEncodedFormEntity(pairs));
        HttpResponse response = httpClient.execute(httpPost);
        Log.e(AnkiDroidApp.TAG, String.format("Bug report posted to %s", url));
        int respCode = response.getStatusLine().getStatusCode();
        switch(respCode) {
            case 200:
                result.success = true;
                result.returnType = respCode;
                result.result = Utils.convertStreamToString(response.getEntity().getContent());
                Log.i(AnkiDroidApp.TAG, String.format("postFeedback OK: %s", result.result));
                break;
            default:
                Log.e(AnkiDroidApp.TAG, String.format("postFeedback failure: %d - %s", response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()));
                result.success = false;
                result.returnType = respCode;
                result.result = response.getStatusLine().getReasonPhrase();
                break;
        }
    } catch (ClientProtocolException ex) {
        Log.e(AnkiDroidApp.TAG, "ClientProtocolException: " + ex.toString());
        result.success = false;
        result.result = ex.toString();
    } catch (IOException ex) {
        Log.e(AnkiDroidApp.TAG, "IOException: " + ex.toString());
        result.success = false;
        result.result = ex.toString();
    }
    return result;
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpPost(org.apache.http.client.methods.HttpPost) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) ClientProtocolException(org.apache.http.client.ClientProtocolException) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) Payload(com.ichi2.async.Connection.Payload)

Example 10 with Result

use of com.ichi2.anki.multimediacard.googleimagesearch.json.Result 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)

Aggregations

IOException (java.io.IOException)7 Intent (android.content.Intent)6 Resources (android.content.res.Resources)6 DeckTask (com.ichi2.async.DeckTask)6 Collection (com.ichi2.libanki.Collection)6 JSONException (org.json.JSONException)6 DialogInterface (android.content.DialogInterface)5 TaskData (com.ichi2.async.DeckTask.TaskData)5 File (java.io.File)5 OnCancelListener (android.content.DialogInterface.OnCancelListener)4 SharedPreferences (android.content.SharedPreferences)4 OnClickListener (android.view.View.OnClickListener)4 NotFoundException (android.content.res.Resources.NotFoundException)3 View (android.view.View)3 Payload (com.ichi2.async.Connection.Payload)3 StyledDialog (com.ichi2.themes.StyledDialog)3 FileNotFoundException (java.io.FileNotFoundException)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 JSONObject (org.json.JSONObject)3