1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  */
9 package org.libreoffice.ui;
10 
11 import android.content.Context;
12 import android.graphics.Bitmap;
13 import android.graphics.BitmapFactory;
14 import android.graphics.Canvas;
15 import android.graphics.Paint;
16 import android.graphics.RectF;
17 import android.graphics.Color;
18 import android.util.AttributeSet;
19 import android.util.Log;
20 import android.view.View;
21 
22 import java.io.File;
23 import java.util.Stack;
24 
25 public class FolderIconView extends View{
26     private String LOGTAG = "FolderIconView";
27 
28     private Paint mPaintBlack;
29     private Paint mPaintGray;
30     private Paint mPaintShadow;
31 
32     private File dir;
33 
FolderIconView(Context context)34     public FolderIconView(Context context) {
35         super(context);
36         initialisePaints();
37     }
FolderIconView(Context context, AttributeSet attrs)38     public FolderIconView(Context context, AttributeSet attrs) {
39         super(context, attrs);
40         initialisePaints();
41     }
FolderIconView(Context context, AttributeSet attrs, int defStyle)42     public FolderIconView(Context context, AttributeSet attrs, int defStyle) {
43         super(context, attrs, defStyle);
44         initialisePaints();
45     }
46 
initialisePaints()47     private void initialisePaints() {
48         mPaintBlack = new Paint();
49         mPaintBlack.setColor(Color.DKGRAY);//Can also use parseColor(String "#aarrggbb")
50         mPaintBlack.setAntiAlias(true);
51 
52         mPaintGray = new Paint();
53         mPaintGray.setColor(Color.GRAY);//Can also use parseColor(String "#aarrggbb")
54         mPaintGray.setAntiAlias(true);
55 
56         mPaintShadow = new Paint();
57         mPaintShadow.setColor(Color.parseColor("#88888888"));
58         mPaintShadow.setAntiAlias(true);
59     }
60 
setDir(File dir)61     public void setDir(File dir) {
62         this.dir = dir;
63     }
64 
65     @Override
onDraw(Canvas canvas)66     protected void onDraw(Canvas canvas) {
67         super.onDraw(canvas);
68         Log.d(LOGTAG, "onDraw");
69         //float width = (float)canvas.getWidth();
70         //float height = (float)canvas.getHeight();
71         float width = (float) this.getWidth();
72         float height = (float) this.getHeight();
73         float centerX = width*0.5f;// centered on horz axis
74         float centerY = height*0.5f;
75         float outerRadius = 0.8f*0.5f*width;
76         float innerRadius = 0.7f*0.5f*width;
77         float thumbHeight = outerRadius*1.25f;
78         float thumbWidth = thumbHeight*(float)(1/Math.sqrt(2));
79         float DZx = 0.2f*outerRadius;
80         float DZy = 0.2f*outerRadius;
81         //Bitmap blankPage = BitmapFactory.decodeResource(getResources(), R.drawable.page);
82         Log.i(LOGTAG, Float.toString(width) + "x" + Float.toString(height));
83         canvas.drawCircle(centerX, centerY, outerRadius, mPaintGray);
84         canvas.drawCircle(centerX, centerY, innerRadius, mPaintBlack);
85         //Either get thumbs from directory or use generic page images
86         //For now just get the first 4 thumbs -> add some checks later
87         if (dir == null)
88             return;//TODO
89         File[] contents = dir.listFiles();//TODO consider filtering thumbs to match grid.
90         if (contents == null)
91             // dir is not a directory,
92             // or user does not have permissions to read it
93             return;
94         Stack<Bitmap> thumbs = new Stack<Bitmap>();
95         BitmapFactory factory = new BitmapFactory();
96         for (File file : contents) {
97             if (!FileUtilities.isThumbnail(file))
98                 continue;
99             thumbs.push(BitmapFactory.decodeFile(file.getAbsolutePath()));//TODO switch to push for semantics
100             if (thumbs.size() > 3)
101                 break;
102         }
103         /*while(thumbs.size() < 4) {// padd out with blanks?
104             thumbs.push(blankPage);
105         }*/
106         Log.i(LOGTAG, Integer.toString(thumbs.size()));
107         //should handle empty folders better
108         //  options:
109         //      don't show?
110         //      show generic LO icons for writer etc
111         //      Show a generic blank page icon
112         if (thumbs.isEmpty())
113             return;
114         /*float left = centerX ;//+ 0.25f*outerRadius;
115         float top = centerY - 0.5f*outerRadius;
116         float right = left + thumbs.get(0).getWidth()*0.4f;
117         float bottom = top + thumbs.get(0).getHeight()*0.4f;
118         RectF dest = new RectF(left, top, right, bottom);
119         RectF shadowBox = new RectF(dest);
120         shadowBox.inset(-1, -1);
121         int size = thumbs.size();
122         for (int i = 1; i <= size; i++) {
123             canvas.drawRect(shadowBox, mPaintShadow);
124             canvas.drawBitmap(thumbs.pop(), null, dest, null);
125             dest.offset(-outerRadius*0.2f, outerRadius*0.1f);
126             shadowBox.offset(-outerRadius*0.2f, outerRadius*0.1f);
127         }*/
128         float left;
129         float top;
130         float right;
131         float bottom;
132         RectF dest;
133         RectF shadowBox;
134         int size;
135         switch(thumbs.size()) {
136             case 0:
137                 break;
138             case 1:
139                 left = centerX - 0.5f*thumbWidth;
140                 top = centerY - 0.5f*thumbHeight;
141                 right = left + thumbWidth;
142                 bottom = top + thumbHeight;
143                 dest = new RectF(left, top, right, bottom);
144                 shadowBox = new RectF(dest);
145                 shadowBox.inset(-1, -1);
146                 canvas.drawRect(shadowBox, mPaintShadow);
147                 canvas.drawBitmap(thumbs.pop(), null, dest, null);
148                 break;
149             case 2:
150                 left = centerX - 0.5f*thumbWidth + 0.5f*DZx;
151                 top = centerY - 0.5f*thumbHeight - 0.5f*DZy;
152                 right = left + thumbWidth;
153                 bottom = top + thumbHeight;
154                 dest = new RectF(left, top, right, bottom);
155                 shadowBox = new RectF(dest);
156                 shadowBox.inset(-1, -1);
157                 size = thumbs.size();
158                 for (int i = 1; i <= size; i++) {
159                     canvas.drawRect(shadowBox, mPaintShadow);
160                     canvas.drawBitmap(thumbs.pop(), null, dest, null);
161                     dest.offset(-DZx, DZy);
162                     shadowBox.offset(-DZx, DZy);
163                 }
164                 break;
165             case 3:
166                 left = centerX - 0.5f*thumbWidth + DZx;
167                 top = centerY - 0.5f*thumbHeight - DZy;
168                 right = left + thumbWidth;
169                 bottom = top + thumbHeight;
170                 dest = new RectF(left, top, right, bottom);
171                 shadowBox = new RectF(dest);
172                 shadowBox.inset(-1, -1);
173                 size = thumbs.size();
174                 for (int i = 1; i <= size; i++) {
175                     canvas.drawRect(shadowBox, mPaintShadow);
176                     canvas.drawBitmap(thumbs.pop(), null, dest, null);
177                     dest.offset(-DZx, DZy);
178                     shadowBox.offset(-DZx, DZy);
179                 }
180                 break;
181             case 4:
182                 left = centerX - 0.5f*thumbWidth + 1.5f*DZx;
183                 top = centerY - 0.5f*thumbHeight - 1.5f*DZy;
184                 right = left + thumbWidth;
185                 bottom = top + thumbHeight;
186                 dest = new RectF(left, top, right, bottom);
187                 shadowBox = new RectF(dest);
188                 shadowBox.inset(-1, -1);
189                 size = thumbs.size();
190                 for (int i = 1; i <= size; i++) {
191                     canvas.drawRect(shadowBox, mPaintShadow);
192                     canvas.drawBitmap(thumbs.pop(), null, dest, null);
193                     dest.offset(-DZx, DZy);
194                     shadowBox.offset(-DZx, DZy);
195                 }
196                 break;
197             default:
198                 break;
199         }
200     }
201 
202 }
203 
204 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
205