Home | History | Annotate | Download | only in testapp
      1 /*
      2  * Copyright 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package androidx.work.integration.testapp;
     17 
     18 import android.os.Handler;
     19 import android.os.Looper;
     20 import android.support.annotation.NonNull;
     21 import android.util.Log;
     22 import android.widget.Toast;
     23 
     24 import androidx.work.Data;
     25 import androidx.work.OneTimeWorkRequest;
     26 import androidx.work.Worker;
     27 
     28 /**
     29  *  A {@link Worker} that shows a given Toast.
     30  */
     31 public class ToastWorker extends Worker {
     32     static final String ARG_MESSAGE = "message";
     33 
     34     /**
     35      * Create a {@link OneTimeWorkRequest.Builder} with the given message.
     36      *
     37      * @param message The toast message to display
     38      * @return A {@link OneTimeWorkRequest.Builder}
     39      */
     40     public static OneTimeWorkRequest.Builder create(String message) {
     41         Data input = new Data.Builder().putString(ARG_MESSAGE, message).build();
     42         return new OneTimeWorkRequest.Builder(ToastWorker.class).setInputData(input);
     43     }
     44 
     45     @Override
     46     public @NonNull Result doWork() {
     47         Data input = getInputData();
     48         final String message = input.getString(ARG_MESSAGE, "completed!");
     49         new Handler(Looper.getMainLooper()).post(new Runnable() {
     50             @Override
     51             public void run() {
     52                 Log.d("ToastWorker", message);
     53                 Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
     54             }
     55         });
     56         return Result.SUCCESS;
     57     }
     58 }
     59