1 /* 2 * Copyright (C) 2015 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 17 package com.example.android.tvleanback.data; 18 19 import android.content.AsyncTaskLoader; 20 import android.content.Context; 21 import android.util.Log; 22 23 import com.example.android.tvleanback.model.Movie; 24 25 import java.util.HashMap; 26 import java.util.List; 27 28 /* 29 * This class asynchronously loads videos from a backend 30 */ 31 public class VideoItemLoader extends AsyncTaskLoader<HashMap<String, List<Movie>>> { 32 33 private static final String TAG = "VideoItemLoader"; 34 private final String mUrl; 35 private Context mContext; 36 37 public VideoItemLoader(Context context, String url) { 38 super(context); 39 mContext = context; 40 mUrl = url; 41 } 42 43 @Override 44 public HashMap<String, List<Movie>> loadInBackground() { 45 try { 46 return VideoProvider.buildMedia(mContext, mUrl); 47 } catch (Exception e) { 48 Log.e(TAG, "Failed to fetch media data", e); 49 return null; 50 } 51 } 52 53 @Override 54 protected void onStartLoading() { 55 super.onStartLoading(); 56 forceLoad(); 57 } 58 59 /** 60 * Handles a request to stop the Loader. 61 */ 62 @Override 63 protected void onStopLoading() { 64 // Attempt to cancel the current load task if possible. 65 cancelLoad(); 66 } 67 68 } 69