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.graphics.Rect;
     21 import android.util.AttributeSet;
     22 import android.view.View;
     23 import android.view.View.OnClickListener;
     24 import android.widget.AdapterView;
     25 import android.widget.Button;
     26 import android.widget.LinearLayout;
     27 import android.widget.AbsListView;
     28 import android.widget.BaseAdapter;
     29 import android.widget.AdapterView.OnItemClickListener;
     30 import android.widget.AdapterView.OnItemLongClickListener;
     31 
     32 import com.android.launcher.R;
     33 
     34 /**
     35  * Represents a set of icons chosen by the user or generated by the system.
     36  */
     37 public class Folder extends LinearLayout implements DragSource, OnItemLongClickListener,
     38         OnItemClickListener, OnClickListener, View.OnLongClickListener {
     39 
     40     protected AbsListView mContent;
     41     protected DragController mDragController;
     42 
     43     protected Launcher mLauncher;
     44 
     45     protected Button mCloseButton;
     46 
     47     protected FolderInfo mInfo;
     48 
     49     /**
     50      * Which item is being dragged
     51      */
     52     protected ShortcutInfo mDragItem;
     53 
     54     /**
     55      * Used to inflate the Workspace from XML.
     56      *
     57      * @param context The application's context.
     58      * @param attrs The attribtues set containing the Workspace's customization values.
     59      */
     60     public Folder(Context context, AttributeSet attrs) {
     61         super(context, attrs);
     62         setAlwaysDrawnWithCacheEnabled(false);
     63     }
     64 
     65     @Override
     66     protected void onFinishInflate() {
     67         super.onFinishInflate();
     68 
     69         mContent = (AbsListView) findViewById(R.id.folder_content);
     70         mContent.setOnItemClickListener(this);
     71         mContent.setOnItemLongClickListener(this);
     72 
     73         mCloseButton = (Button) findViewById(R.id.folder_close);
     74         mCloseButton.setOnClickListener(this);
     75         mCloseButton.setOnLongClickListener(this);
     76     }
     77 
     78     public void onItemClick(AdapterView parent, View v, int position, long id) {
     79         ShortcutInfo app = (ShortcutInfo) parent.getItemAtPosition(position);
     80         int[] pos = new int[2];
     81         v.getLocationOnScreen(pos);
     82         app.intent.setSourceBounds(new Rect(pos[0], pos[1],
     83                 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
     84         mLauncher.startActivitySafely(app.intent, app);
     85     }
     86 
     87     public void onClick(View v) {
     88         mLauncher.closeFolder(this);
     89     }
     90 
     91     public boolean onLongClick(View v) {
     92         mLauncher.closeFolder(this);
     93         mLauncher.showRenameDialog(mInfo);
     94         return true;
     95     }
     96 
     97     public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
     98         if (!view.isInTouchMode()) {
     99             return false;
    100         }
    101 
    102         ShortcutInfo app = (ShortcutInfo) parent.getItemAtPosition(position);
    103 
    104         mDragController.startDrag(view, this, app, DragController.DRAG_ACTION_COPY);
    105         mLauncher.closeFolder(this);
    106         mDragItem = app;
    107 
    108         return true;
    109     }
    110 
    111     public void setDragController(DragController dragController) {
    112         mDragController = dragController;
    113     }
    114 
    115     public void onDropCompleted(View target, boolean success) {
    116     }
    117 
    118     /**
    119      * Sets the adapter used to populate the content area. The adapter must only
    120      * contains ShortcutInfo items.
    121      *
    122      * @param adapter The list of applications to display in the folder.
    123      */
    124     void setContentAdapter(BaseAdapter adapter) {
    125         mContent.setAdapter(adapter);
    126     }
    127 
    128     void notifyDataSetChanged() {
    129         ((BaseAdapter) mContent.getAdapter()).notifyDataSetChanged();
    130     }
    131 
    132     void setLauncher(Launcher launcher) {
    133         mLauncher = launcher;
    134     }
    135 
    136     /**
    137      * @return the FolderInfo object associated with this folder
    138      */
    139     FolderInfo getInfo() {
    140         return mInfo;
    141     }
    142 
    143     // When the folder opens, we need to refresh the GridView's selection by
    144     // forcing a layout
    145     void onOpen() {
    146         mContent.requestLayout();
    147     }
    148 
    149     void onClose() {
    150         final Workspace workspace = mLauncher.getWorkspace();
    151         workspace.getChildAt(workspace.getCurrentScreen()).requestFocus();
    152     }
    153 
    154     void bind(FolderInfo info) {
    155         mInfo = info;
    156         mCloseButton.setText(info.title);
    157     }
    158 }
    159