summaryrefslogtreecommitdiff
path: root/android/experimental/LOAndroid3/src/java/org/libreoffice/ui/FolderIconView.java
blob: b0211b13ac258775b59313a2606409a51b5e191a (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
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package org.libreoffice.ui;

import org.libreoffice.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.io.File;
import java.util.Stack;

public class FolderIconView extends View{
    private String TAG = "FolderIconView";

    private Paint mPaintBlack;
    private Paint mPaintGray;
    private Paint mPaintShadow;

    private File dir;

    public FolderIconView(Context context ) {
        super(context);
        initialisePaints();
    }
    public FolderIconView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialisePaints();
    }
    public FolderIconView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initialisePaints();
    }

    private void initialisePaints(){
        mPaintBlack = new Paint();
        mPaintBlack.setColor( Color.DKGRAY );//Can also use parseColor( String "#aarrggbb")
        mPaintBlack.setAntiAlias( true );

        mPaintGray = new Paint();
        mPaintGray.setColor( Color.GRAY );//Can also use parseColor( String "#aarrggbb")
        mPaintGray.setAntiAlias( true );

        mPaintShadow = new Paint();
        mPaintShadow.setColor( Color.parseColor( "#88888888") );
        mPaintShadow.setAntiAlias( true );
    }

    public void setDir( File dir ){
        this.dir = dir;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.d( TAG, "onDraw");
        //float width = (float)canvas.getWidth();
        //float height = (float)canvas.getHeight();
        float width = (float)this.getWidth();
        float height = (float)this.getHeight();
        float centerX = width*0.5f;// centered on horz axis
        float centerY = height*0.5f;
        float outerRadius = 0.8f*0.5f* width;
        float innerRadius = 0.7f*0.5f* width;
        float thumbHeight = outerRadius*1.25f;
        float thumbWidth = thumbHeight*(float)(1/Math.sqrt(2));
        float DZx = 0.2f*outerRadius;
        float DZy = 0.2f*outerRadius;
        //Bitmap blankPage = BitmapFactory.decodeResource( getResources() , R.drawable.page );
        Log.i( TAG , Float.toString( width ) + " X " + Float.toString( height ) );
        canvas.drawCircle( centerX , centerY , outerRadius , mPaintGray );
        canvas.drawCircle( centerX , centerY , innerRadius , mPaintBlack );
        //Either get thumbs from directory or use generic page images
        //For now just get the first 4 thumbs -> add some checks later
        if( dir == null )
            return;//TODO
        File[] contents = dir.listFiles();//TODO consider filtering thumbs to match grid.
        if( contents == null )
            // dir is not a directory,
            // or user does not have permissions to read it
            return;
        Stack<Bitmap> thumbs = new Stack<Bitmap>();
        BitmapFactory factory = new BitmapFactory();
        for( File file : contents ){
            if( !FileUtilities.isThumbnail(file) )
                continue;
            thumbs.push( factory.decodeFile( file.getAbsolutePath() ) );//TODO switch to push for semantics
            if( thumbs.size() > 3 )
                break;
        }
        /*while( thumbs.size() < 4 ){// padd out with blanks?
            thumbs.push( blankPage );
        }*/
        Log.i( TAG, Integer.toString( thumbs.size() ) );
        //should handle empty folders better
        //  options:
        //      don't show?
        //      show generic LO icons for writer etc
        //      Show a generic blank page icon
        if( thumbs.isEmpty() )
            return;
        /*float left = centerX ;//+ 0.25f*outerRadius;
        float top = centerY - 0.5f*outerRadius;
        float right = left + thumbs.get(0).getWidth()*0.4f;
        float bottom = top + thumbs.get(0).getHeight()*0.4f;
        RectF dest = new RectF( left, top , right , bottom );
        RectF shadowBox = new RectF(dest);
        shadowBox.inset( -1 , -1 );
        int size = thumbs.size();
        for( int i = 1 ; i <= size ; i++ ){
            canvas.drawRect( shadowBox , mPaintShadow);
            canvas.drawBitmap( thumbs.pop() , null , dest , null);
            dest.offset( -outerRadius*0.2f , outerRadius*0.1f );
            shadowBox.offset( -outerRadius*0.2f , outerRadius*0.1f );
        }*/
        float left;
        float top;
        float right;
        float bottom;
        RectF dest;
        RectF shadowBox;
        int size;
        switch( thumbs.size() ){
            case 0:
                break;
            case 1:
                left = centerX - 0.5f*thumbWidth;
                top = centerY - 0.5f*thumbHeight;
                right = left + thumbWidth;
                bottom = top + thumbHeight;
                dest = new RectF( left, top , right , bottom );
                shadowBox = new RectF(dest);
                shadowBox.inset( -1 , -1 );
                canvas.drawRect( shadowBox , mPaintShadow);
                canvas.drawBitmap( thumbs.pop() , null , dest , null);
                break;
            case 2:
                left = centerX - 0.5f*thumbWidth + 0.5f*DZx;
                top = centerY - 0.5f*thumbHeight - 0.5f*DZy;
                right = left + thumbWidth;
                bottom = top + thumbHeight;
                dest = new RectF( left, top , right , bottom );
                shadowBox = new RectF(dest);
                shadowBox.inset( -1 , -1 );
                size = thumbs.size();
                for( int i = 1 ; i <= size ; i++ ){
                    canvas.drawRect( shadowBox , mPaintShadow);
                    canvas.drawBitmap( thumbs.pop() , null , dest , null);
                    dest.offset( -DZx , DZy );
                    shadowBox.offset( -DZx , DZy );
                }
                break;
            case 3:
                left = centerX - 0.5f*thumbWidth + DZx;
                top = centerY - 0.5f*thumbHeight - DZy;
                right = left + thumbWidth;
                bottom = top + thumbHeight;
                dest = new RectF( left, top , right , bottom );
                shadowBox = new RectF(dest);
                shadowBox.inset( -1 , -1 );
                size = thumbs.size();
                for( int i = 1 ; i <= size ; i++ ){
                    canvas.drawRect( shadowBox , mPaintShadow);
                    canvas.drawBitmap( thumbs.pop() , null , dest , null);
                    dest.offset( -DZx , DZy );
                    shadowBox.offset( -DZx , DZy );
                }
                break;
            case 4:
                left = centerX - 0.5f*thumbWidth + 1.5f*DZx;
                top = centerY - 0.5f*thumbHeight - 1.5f*DZy;
                right = left + thumbWidth;
                bottom = top + thumbHeight;
                dest = new RectF( left, top , right , bottom );
                shadowBox = new RectF(dest);
                shadowBox.inset( -1 , -1 );
                size = thumbs.size();
                for( int i = 1 ; i <= size ; i++ ){
                    canvas.drawRect( shadowBox , mPaintShadow);
                    canvas.drawBitmap( thumbs.pop() , null , dest , null);
                    dest.offset( -DZx , DZy );
                    shadowBox.offset( -DZx , DZy );
                }
                break;
            default:
                break;
        }
        //test

        return;
    }

}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */