Search in sources :

Example 1 with UploadResponse

use of com.thebluealliance.imgur.responses.UploadResponse in project the-blue-alliance-android by the-blue-alliance.

the class ImgurSuggestionService method onHandleIntent.

@Override
protected void onHandleIntent(Intent intent) {
    TbaLogger.d("IMGUR SERVICE START");
    String filepath = intent.getStringExtra(EXTRA_FILEPATH);
    String title = intent.getStringExtra(EXTRA_TITLE);
    String description = intent.getStringExtra(EXTRA_DESCRIPTION);
    String teamKey = intent.getStringExtra(EXTRA_TEAMKEY);
    int year = intent.getIntExtra(EXTRA_YEAR, 0);
    ImgurUploadNotification notification = new ImgurUploadNotification(getApplicationContext());
    notification.onUploadStarting();
    boolean successful = true;
    // Get a wake lock so any long uploads will not be interrupted
    PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
    PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
    wakeLock.acquire();
    // TODO should we catch each exception individually and handle them better (e.g. retyr?)
    try {
        String authToken = getAuthHeader();
        File file = new File(filepath);
        RequestBody body = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        RequestBody titlePart = RequestBody.create(MediaType.parse("text/plain"), title);
        RequestBody descPart = RequestBody.create(MediaType.parse("text/plain"), description);
        Response<UploadResponse> response = mImgurApi.uploadImage(authToken, titlePart, descPart, body).execute();
        if (response != null && response.isSuccessful()) {
            UploadResponse uploadResponse = response.body();
            TbaLogger.d("Uploaded imgur image: " + uploadResponse.data.link);
            String link = uploadResponse.data.link;
            String deletehash = uploadResponse.data.deletehash;
            TbaLogger.d("Imgur link: " + link);
            // Do suggestion
            String authHeader = mGceAuthController.getAuthHeader();
            ModelsMobileApiMessagesMediaSuggestionMessage message = buildSuggestionMessage(teamKey, year, link, deletehash);
            Response<ModelsMobileApiMessagesBaseResponse> suggestionResponse = mTeamMediaApi.suggestion(authHeader, message).execute();
            if (suggestionResponse != null && suggestionResponse.isSuccessful()) {
            // Yay, everything worked!
            } else {
                // Crap
                // TODO handle this
                successful = false;
            }
        } else {
            TbaLogger.e("Error uploading imgur image\n" + response.code() + " " + response.message());
            successful = false;
        }
    } catch (Exception e) {
        // Something broke
        successful = false;
        e.printStackTrace();
    } finally {
        if (successful) {
            notification.onUploadSuccess();
        } else {
            notification.onUploadFailure();
        }
        // Delete the temp cached image
        new File(filepath).delete();
        wakeLock.release();
    }
}
Also used : ModelsMobileApiMessagesMediaSuggestionMessage(com.appspot.tbatv_prod_hrd.model.ModelsMobileApiMessagesMediaSuggestionMessage) AndroidEntryPoint(dagger.hilt.android.AndroidEntryPoint) PowerManager(android.os.PowerManager) UploadResponse(com.thebluealliance.imgur.responses.UploadResponse) ModelsMobileApiMessagesBaseResponse(com.appspot.tbatv_prod_hrd.model.ModelsMobileApiMessagesBaseResponse) File(java.io.File) RequestBody(okhttp3.RequestBody)

Aggregations

PowerManager (android.os.PowerManager)1 ModelsMobileApiMessagesBaseResponse (com.appspot.tbatv_prod_hrd.model.ModelsMobileApiMessagesBaseResponse)1 ModelsMobileApiMessagesMediaSuggestionMessage (com.appspot.tbatv_prod_hrd.model.ModelsMobileApiMessagesMediaSuggestionMessage)1 UploadResponse (com.thebluealliance.imgur.responses.UploadResponse)1 AndroidEntryPoint (dagger.hilt.android.AndroidEntryPoint)1 File (java.io.File)1 RequestBody (okhttp3.RequestBody)1