Search in sources :

Example 1 with ModelsMobileApiMessagesMediaSuggestionMessage

use of com.appspot.tbatv_prod_hrd.model.ModelsMobileApiMessagesMediaSuggestionMessage in project the-blue-alliance-android by the-blue-alliance.

the class TbaSuggestionController method suggest.

/**
 * <b>MUST BE CALLED FROM BACKGROUND THREAD</b>
 * Uses {@link GceAuthController#getAuthHeader()}
 */
@WorkerThread
public void suggest(String teamKey, int year, String link, String deletehash) {
    String authHeader = mGceAuthController.getAuthHeader();
    ModelsMobileApiMessagesMediaSuggestionMessage message = buildSuggestionMessage(teamKey, year, link, deletehash);
    Call<ModelsMobileApiMessagesBaseResponse> request = mTeamMediaApi.suggestion(authHeader, message);
    request.enqueue(new Callback<ModelsMobileApiMessagesBaseResponse>() {

        @Override
        public void onResponse(Call<ModelsMobileApiMessagesBaseResponse> call, Response<ModelsMobileApiMessagesBaseResponse> response) {
        // TODO stuff
        }

        @Override
        public void onFailure(Call<ModelsMobileApiMessagesBaseResponse> call, Throwable t) {
        // TODO stuff
        }
    });
}
Also used : ModelsMobileApiMessagesBaseResponse(com.appspot.tbatv_prod_hrd.model.ModelsMobileApiMessagesBaseResponse) ModelsMobileApiMessagesMediaSuggestionMessage(com.appspot.tbatv_prod_hrd.model.ModelsMobileApiMessagesMediaSuggestionMessage) WorkerThread(androidx.annotation.WorkerThread)

Example 2 with ModelsMobileApiMessagesMediaSuggestionMessage

use of com.appspot.tbatv_prod_hrd.model.ModelsMobileApiMessagesMediaSuggestionMessage in project the-blue-alliance-android by the-blue-alliance.

the class ImgurSuggestionService method buildSuggestionMessage.

private static ModelsMobileApiMessagesMediaSuggestionMessage buildSuggestionMessage(String teamKey, int year, String imgurLink, String deleteHash) {
    ModelsMobileApiMessagesMediaSuggestionMessage message = new ModelsMobileApiMessagesMediaSuggestionMessage();
    message.media_url = imgurLink;
    message.reference_key = teamKey;
    message.year = year;
    message.reference_type = TEAM_LINK;
    message.details_json = getDetailsJson(deleteHash);
    return message;
}
Also used : ModelsMobileApiMessagesMediaSuggestionMessage(com.appspot.tbatv_prod_hrd.model.ModelsMobileApiMessagesMediaSuggestionMessage)

Example 3 with ModelsMobileApiMessagesMediaSuggestionMessage

use of com.appspot.tbatv_prod_hrd.model.ModelsMobileApiMessagesMediaSuggestionMessage 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)

Example 4 with ModelsMobileApiMessagesMediaSuggestionMessage

use of com.appspot.tbatv_prod_hrd.model.ModelsMobileApiMessagesMediaSuggestionMessage in project the-blue-alliance-android by the-blue-alliance.

the class TbaSuggestionController method buildSuggestionMessage.

private static ModelsMobileApiMessagesMediaSuggestionMessage buildSuggestionMessage(String teamKey, int year, String imgurLink, String deleteHash) {
    ModelsMobileApiMessagesMediaSuggestionMessage message = new ModelsMobileApiMessagesMediaSuggestionMessage();
    message.media_url = imgurLink;
    message.reference_key = teamKey;
    message.year = year;
    message.reference_type = TEAM_LINK;
    message.details_json = getDetailsJson(deleteHash);
    return message;
}
Also used : ModelsMobileApiMessagesMediaSuggestionMessage(com.appspot.tbatv_prod_hrd.model.ModelsMobileApiMessagesMediaSuggestionMessage)

Aggregations

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