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