Home | History | Annotate | Download | only in launcher2
      1 /*
      2  * Copyright (C) 2008 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.android.launcher2;
     18 
     19 import android.content.Context;
     20 import android.content.Intent;
     21 import android.util.AttributeSet;
     22 import android.view.LayoutInflater;
     23 import android.view.View;
     24 import android.widget.AdapterView;
     25 import android.net.Uri;
     26 import android.provider.LiveFolders;
     27 import android.os.AsyncTask;
     28 import android.database.Cursor;
     29 
     30 import java.lang.ref.WeakReference;
     31 
     32 import com.android.launcher.R;
     33 
     34 public class LiveFolder extends Folder {
     35     private AsyncTask<LiveFolderInfo,Void,Cursor> mLoadingTask;
     36 
     37     public LiveFolder(Context context, AttributeSet attrs) {
     38         super(context, attrs);
     39     }
     40 
     41     static LiveFolder fromXml(Context context, FolderInfo folderInfo) {
     42         final int layout = isDisplayModeList(folderInfo) ?
     43                 R.layout.live_folder_list : R.layout.live_folder_grid;
     44         return (LiveFolder) LayoutInflater.from(context).inflate(layout, null);
     45     }
     46 
     47     private static boolean isDisplayModeList(FolderInfo folderInfo) {
     48         return ((LiveFolderInfo) folderInfo).displayMode ==
     49                 LiveFolders.DISPLAY_MODE_LIST;
     50     }
     51 
     52     @Override
     53     public void onItemClick(AdapterView parent, View v, int position, long id) {
     54         LiveFolderAdapter.ViewHolder holder = (LiveFolderAdapter.ViewHolder) v.getTag();
     55 
     56         if (holder.useBaseIntent) {
     57             final Intent baseIntent = ((LiveFolderInfo) mInfo).baseIntent;
     58             if (baseIntent != null) {
     59                 final Intent intent = new Intent(baseIntent);
     60                 Uri uri = baseIntent.getData();
     61                 uri = uri.buildUpon().appendPath(Long.toString(holder.id)).build();
     62                 intent.setData(uri);
     63                 mLauncher.startActivitySafely(intent, "(position=" + position + ", id=" + id + ")");
     64             }
     65         } else if (holder.intent != null) {
     66             mLauncher.startActivitySafely(holder.intent,
     67                     "(position=" + position + ", id=" + id + ")");
     68         }
     69     }
     70 
     71     @Override
     72     public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
     73         return false;
     74     }
     75 
     76     void bind(FolderInfo info) {
     77         super.bind(info);
     78         if (mLoadingTask != null && mLoadingTask.getStatus() == AsyncTask.Status.RUNNING) {
     79             mLoadingTask.cancel(true);
     80         }
     81         mLoadingTask = new FolderLoadingTask(this).execute((LiveFolderInfo) info);
     82     }
     83 
     84     @Override
     85     void onOpen() {
     86         super.onOpen();
     87         requestFocus();
     88     }
     89 
     90     @Override
     91     void onClose() {
     92         super.onClose();
     93         if (mLoadingTask != null && mLoadingTask.getStatus() == AsyncTask.Status.RUNNING) {
     94             mLoadingTask.cancel(true);
     95         }
     96 
     97         // The adapter can be null if onClose() is called before FolderLoadingTask
     98         // is done querying the provider
     99         final LiveFolderAdapter adapter = (LiveFolderAdapter) mContent.getAdapter();
    100         if (adapter != null) {
    101             adapter.cleanup();
    102         }
    103     }
    104 
    105     static class FolderLoadingTask extends AsyncTask<LiveFolderInfo, Void, Cursor> {
    106         private final WeakReference<LiveFolder> mFolder;
    107         private LiveFolderInfo mInfo;
    108 
    109         FolderLoadingTask(LiveFolder folder) {
    110             mFolder = new WeakReference<LiveFolder>(folder);
    111         }
    112 
    113         protected Cursor doInBackground(LiveFolderInfo... params) {
    114             final LiveFolder folder = mFolder.get();
    115             if (folder != null) {
    116                 mInfo = params[0];
    117                 return LiveFolderAdapter.query(folder.mLauncher, mInfo);
    118             }
    119             return null;
    120         }
    121 
    122         @Override
    123         protected void onPostExecute(Cursor cursor) {
    124             if (!isCancelled()) {
    125                 if (cursor != null) {
    126                     final LiveFolder folder = mFolder.get();
    127                     if (folder != null) {
    128                         final Launcher launcher = folder.mLauncher;
    129                         folder.setContentAdapter(new LiveFolderAdapter(launcher, mInfo, cursor));
    130                     }
    131                 }
    132             } else if (cursor != null) {
    133                 cursor.close();
    134             }
    135         }
    136     }
    137 }
    138