summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher3/RemoteFolder.java
blob: 136489466256a95a0a614a881a1348fb81bc6f4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package com.android.launcher3;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

/**
 * Created by tmiller on 11/24/15.
 */
public class RemoteFolder extends Folder {

    public static final String TAG = "RemoteFolder";
    public static final String REMOTE_FOLDER_ENABLED = "remote_folder_enabled";
    private ScrollView mContentScrollView;
    private ImageView mFolderInfo;
    private TextView mFolderHelpText;
    private Button mCloseInfoButton;
    private View mFolderInfoContainer;

    private int mFolderInfoContainerHeight;
    private int mHelpTextHeight;
    private int mHelpTextWidth;
    private int mButtonHeight;
    private int mFolderInfoIconHeight;
    /**
     * Used to inflate the Workspace from XML.
     *
     * @param context The application's context.
     * @param attrs   The attribtues set containing the Workspace's customization values.
     */
    public RemoteFolder(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * Creates a new UserFolder, inflated from R.layout.remote_folder.
     *
     * @param context The application's context.
     *
     * @return A new UserFolder.
     */
    static RemoteFolder fromXml(Context context) {
        return (RemoteFolder) LayoutInflater.from(context).inflate(R.layout.remote_folder, null);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        int measureSpec = MeasureSpec.UNSPECIFIED;

        mContentScrollView = (ScrollView) findViewById(R.id.scroll_view);

        mFolderInfoContainer = findViewById(R.id.folder_info_container);
        mFolderInfoContainer.measure(measureSpec, measureSpec);
        mFolderInfoContainerHeight = mFolderInfoContainer.getMeasuredHeight();

        mFolderInfo = (ImageView) findViewById(R.id.folder_info);
        mFolderInfo.measure(measureSpec, measureSpec);
        mFolderInfoIconHeight = mFolderInfo.getMeasuredHeight();
        mFolderInfo.setOnClickListener(this);

        mFolderHelpText = (TextView) findViewById(R.id.help_text_view);
        mFolderHelpText.setText(getResources().getString(R.string.recommendations_help_text));
        mFolderHelpText.measure(measureSpec, measureSpec);
        mHelpTextHeight = (mFolderHelpText.getLineHeight() * mFolderHelpText.getLineCount()) +
                mFolderHelpText.getPaddingTop() + mFolderHelpText.getPaddingBottom();
        mHelpTextWidth = mFolderHelpText.getMeasuredWidth();
        mFolderHelpText.setVisibility(GONE);

        mCloseInfoButton = (Button) findViewById(R.id.close_info_button);
        mCloseInfoButton.setText(getResources().getString(R.string.close));
        mCloseInfoButton.measure(measureSpec, measureSpec);
        mButtonHeight = mCloseInfoButton.getMeasuredHeight();
        mCloseInfoButton.setOnClickListener(this);
    }

    protected int getFolderHeight() {
        if (mFolderHelpText.getVisibility() == VISIBLE) {
            mHelpTextHeight = (mFolderHelpText.getLineHeight() * mFolderHelpText.getLineCount()) +
                    mFolderHelpText.getPaddingTop() + mFolderHelpText.getPaddingBottom();

            int height = getPaddingTop() + getPaddingBottom() + mFolderInfoIconHeight
                    + mHelpTextHeight + mButtonHeight;
            return height;
        } else {
            int height = getPaddingTop() + getPaddingBottom() + mFolderInfoIconHeight
                    + getContentAreaHeight();
            return height;

        }
    }

    private int getFolderWidth() {
        if (mFolderHelpText.getVisibility() == VISIBLE) {
            DisplayMetrics displayMetrics = this.getResources().getDisplayMetrics();
            int screenWidth = displayMetrics.widthPixels;
            int width = Math.min(mHelpTextWidth, screenWidth - getPaddingLeft() - getPaddingRight());
            return width;
        } else {
            int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
            return width;
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO (Tyson): clean this up before merging into main CM branch
        int width = getFolderWidth();
        int height = getFolderHeight();
        int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(getContentAreaWidth(),
                MeasureSpec.EXACTLY);
        int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(getContentAreaHeight(),
                MeasureSpec.EXACTLY);

        if (LauncherAppState.isDisableAllApps()) {
            // Don't cap the height of the content to allow scrolling.
            mContent.setFixedSize(getContentAreaWidth(), mContent.getDesiredHeight());
        } else {
            mContent.setFixedSize(getContentAreaWidth(), getContentAreaHeight());
        }
        mContentScrollView.measure(contentAreaWidthSpec, contentAreaHeightSpec);

        if (mFolderHelpText.getVisibility() == VISIBLE) {
            mHelpTextHeight = (mFolderHelpText.getLineHeight() * mFolderHelpText.getLineCount()) +
                    mFolderHelpText.getPaddingTop() + mFolderHelpText.getPaddingBottom();

            mFolderHelpText.measure(contentAreaWidthSpec, MeasureSpec.makeMeasureSpec(
                    mHelpTextHeight, MeasureSpec.EXACTLY));
            mFolderInfoContainer.measure(contentAreaWidthSpec,
                    MeasureSpec.makeMeasureSpec(
                            mFolderInfoIconHeight + mHelpTextHeight + mButtonHeight, MeasureSpec.EXACTLY));
            mCloseInfoButton.measure(contentAreaWidthSpec,
                    MeasureSpec.makeMeasureSpec(mButtonHeight, MeasureSpec.EXACTLY));
        } else {
            mHelpTextHeight = 0;
            mFolderHelpText.measure(contentAreaWidthSpec, MeasureSpec.makeMeasureSpec(
                    mHelpTextHeight, MeasureSpec.EXACTLY));
            mFolderInfoContainer.measure(contentAreaWidthSpec,
                    MeasureSpec.makeMeasureSpec(mFolderInfoIconHeight, MeasureSpec.EXACTLY));
            mCloseInfoButton.measure(contentAreaWidthSpec,
                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.EXACTLY));
        }

        Log.e(TAG, "onMeasure(), width: " + width +  ", height:" + height);

        setMeasuredDimension(width, height);
    }

    public void onClick(View v) {
        Object tag = v.getTag();
        if (tag instanceof ShortcutInfo) {
            mLauncher.onClick(v);
        }

        switch (v.getId()) {
            case R.id.folder_info:
                toggleInfoPane();
                break;
            case R.id.close_info_button:
                mLauncher.closeFolder();
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onLongClick(View v) {
        // Eat the event without doing anything. We do not allow users to remove items.
        return true;
    }

    private void toggleInfoPane() {
        if (mFolderHelpText.getVisibility() == VISIBLE) {
            // info ImageView becomes a close "X" when the help text is showing, handle accordingly
            mContentScrollView.setVisibility(VISIBLE);
            mContent.setVisibility(VISIBLE);

            mFolderHelpText.setVisibility(GONE);

            mCloseInfoButton.setVisibility(GONE);

            mFolderInfo.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher_info_normal_holo));

        } else {
            // show the info to the user about remote folders, including the option to disable it
            mContentScrollView.setVisibility(GONE);
            mContent.setVisibility(GONE);

            mFolderHelpText.setVisibility(VISIBLE);

            mCloseInfoButton.setVisibility(VISIBLE);

            mFolderInfo.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher_clear_normal_holo));
        }
        this.invalidate();
    }

    @Override
    public void animateClosed(boolean animate) {
        super.animateClosed(animate);
        mFolderHelpText.setVisibility(GONE);
        mCloseInfoButton.setVisibility(GONE);
        mContentScrollView.setVisibility(VISIBLE);
        mFolderInfo.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher_info_normal_holo));
    }

    @Override
    public void animateOpen(Workspace workspace, int[] folderTouch) {
        super.animateOpen(workspace, folderTouch);

        mFolderHelpText.setText(getResources().getString(R.string.recommendations_help_text));
        mFolderHelpText.setVisibility(GONE);
        mCloseInfoButton.setVisibility(GONE);
        mContentScrollView.setVisibility(VISIBLE);
        mContent.setVisibility(VISIBLE);
        mFolderInfo.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher_info_normal_holo));
        this.invalidate();
    }
}