summaryrefslogtreecommitdiffstats
path: root/src/net/hoopajoo/android/SoftKeys/Generator.java
blob: ace3ee1e56e2357b9726204f1753220427cd5b72 (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
 *
 *  Copyright (c) 2010 Steve Slaven
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
*/
package net.hoopajoo.android.SoftKeys;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Shader;
import android.graphics.Bitmap.Config;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

public class Generator {
    public static int defaultIconSize( Context c ) {
        final Resources resources = c.getResources();
        return( (int) resources.getDimension( android.R.dimen.app_icon_size ) );
    }

    public static int scaledIconSize( Context c, int iconSize, float iconScale ) {
        if( iconSize == 0 ) {
            // default icon size
            iconSize = defaultIconSize( c );
        }
        
        iconSize = (int)(iconSize * iconScale);
        return( iconSize );
    }
    
    public static Theme currentTheme( Context c ) {
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences( c );
        Theme theme = new Theme( c, settings.getString( "theme", null ) );
        return( theme );
    }
    
    public static View createButtonContainer( Context c, int iconSize, float iconScale, String prefix, ViewGroup root ) {
        return createButtonContainer( c, iconSize, iconScale, prefix, root, null );
    }
    
    // this assembles a generic button_container that can be inserted into whatever layout
    public static View createButtonContainer( Context c, int iconSize, float iconScale, String prefix, ViewGroup root, int[] buttons ) {
        Theme theme = currentTheme( c );
        iconSize = scaledIconSize( c, iconSize, iconScale );
        
        // we start with some kind of viewgroup (linearlayout,etc)
        ViewGroup orig_container = (ViewGroup)theme.inflateLayout( c,
                new String[] { prefix + "_button_container", "button_container" }
            , root, false );
        
        // If they have specified a view id to use for insertion then switch to that now
        // this way they can create some special view group with other decorations, and
        // specify the actual child viewgroup we need to add the buttons too (like if they
        // want to stack some images or something to create a composite background using a
        // frameview)
        // Most themes probably don't need this but I figure maybe someone will
        ViewGroup container = orig_container;
        String button_container_name = theme.getString( new String[] {
                prefix + "_button_container_id",
                "button_container_id"
        } );
        
        if( button_container_name != null ) {
            container = (ViewGroup)orig_container.findViewById( theme.getId(
                    new String[] { button_container_name } ) );
            
            // if it's null just go back to main container
            if( container == null ) {
                container = orig_container;
            }
        }

        container.setId( R.id.button_container );
        applyContainerExtras( container, prefix, theme, iconSize );
        
        // now we add the buttons
        if( buttons == null ) {
            buttons = new int[] { 
                R.id.menu, R.id.home,
                R.id.back, R.id.search, 
                R.id.volume_down, R.id.volume_up,
                R.id.sleep,
                R.id.settings, R.id.exit };
        }

        for( int i : buttons ) {
            String name = "unknown";
            switch( i ) {
                case R.id.menu:
                    name = "menu";
                    break;
                    
                case R.id.home:
                    name = "home";
                    break;
                    
                case R.id.back:
                    name = "back";
                    break;
                    
                case R.id.search:
                    name = "search";
                    break;
                    
                case R.id.settings:
                    name = "settings";
                    break;
                    
                case R.id.sleep:
                    name = "sleep";
                    break;
                    
                case R.id.volume_up:
                    name = "volume_up";
                    break;
                    
                case R.id.volume_down:
                    name = "volume_down";
                    break;
                    
                case R.id.custom1:
                    name = "custom1";
                    break;
                    
                case R.id.custom2:
                    name = "custom2";
                    break;
                    
                case R.id.exit:
                    name = "exit";
                    break;
                    
                case R.id.popper:
                    name = "popper";
                    break;
            }
            
            // This allows complex view overriding like the button container
            // the final view we settle on must be an ImageButton (either specified by the
            // theme or just the root element of the xml)
            View orig_b = theme.inflateLayout( c,
                    new String[] { prefix + "_button_" + name, 
                            prefix + "_button", "button_" + name, "button" }
                , container, false );
            
            String button_name = theme.getString( new String[] {
                    prefix + "_button_id",
                    "button_id"
            } );
            
            ImageButton b = null;
            if( button_name != null ) {
                b = (ImageButton)orig_b.findViewById( theme.getId(
                        new String[] { button_name } ) );                
            }
            
            if( b == null ) {
                b = (ImageButton)orig_b;
            }
            
            b.setId( i );

            applyButtonExtras( b, prefix, name, theme, iconSize );
            
            container.addView( orig_b );
        }
        
        // add to root
        if( root != null ) {
            root.addView( orig_container );
        }
        
        return( orig_container );
    }
    
    // this will return a new drawable scaled to the new size, so you don't have to mutable the source
    public static Drawable resizeImage( Drawable d, int w, int h) {
        int width = d.getIntrinsicWidth();
        int height = d.getIntrinsicHeight();

        // catch colors/etc
        if( width < 1 ) {
            width = 1;
        }

        if( height < 1 ) {
            height = 1;
        }

        // if w/h is zero them it means to scale based on the non-zero one and
        // maintain aspect
        if( w == 0 ) {
            w = (int)( (float)h * width / height );
        }else if( h == 0 ) {
            h = (int)( (float)w * height / width );
        }

        Bitmap b;
        if( d instanceof BitmapDrawable ) {
            // I found that the resources are already bitmapdrawables so we can
            // do this,
            // I assume it it's not created from a bitmap like it's a shape or
            // something
            // then this won't work?
            b = ( (BitmapDrawable)d ).getBitmap();
        }else {
            // this was the way more people said to do it, just render the
            // drawable to a canvas
            // backed by your dest bitmap. I assume if you're using a
            // bitmapdrawable
            // then this is slower than just pulling in the drawable backed
            // bitmap
            d.mutate(); // we change the setbounds() so lets not mess with the
            // original
            b = Bitmap.createBitmap( width, height, Config.ARGB_8888 );
            Canvas c = new Canvas( b );
            d.setBounds( 0, 0, width, height );
            d.draw( c );
        }
        
        float scaleWidth = ((float) w) / width;
        float scaleHeight = ((float) h) / height;
        Matrix matrix = new Matrix();
        matrix.postScale( scaleWidth, scaleHeight);

        BitmapDrawable ret = new BitmapDrawable( Bitmap.createBitmap(b, 0, 0, width, height, matrix, true) );

        // copy tile mode
        if( d instanceof BitmapDrawable ) {
            ret.setTileModeXY( ( (BitmapDrawable)d ).getTileModeX(), ( (BitmapDrawable)d ).getTileModeY() );
        }
        return ret;
    }
    
    
    /// NOTE: using any tile mode CLAMP seems to crash the emulator
    //        looking around online some people are saying it crashes their stuff
    //        too, don't know who's to blame but it seems like avoiding
    //        clamp is a good idea if you want compatibility
    // tiling is specified as mode,mode for x/y tiling, or just mode to
    // specify both x and y tiling
    private static void applyTiling( Drawable d, String tilemode ) {
        // check if they specify tiling, this will override tiling in an xml bitmap
        // it's mostly here in case they want just one dimension of tiling instead of both
        if( d instanceof BitmapDrawable ) {
            BitmapDrawable bm = (BitmapDrawable)d;
            // according to docs -1 is the disabled mode but doesn't exist as a constant
            // so I'm hoping that setting it null acheives the same effect
            Shader.TileMode[] tms = new Shader.TileMode[] { null, null };
            
            if( tilemode != null ) {
                String[] modes = new String[] { tilemode, tilemode };
                if( tilemode.contains( "," ) ) {
                    modes = tilemode.split( "," );
                }
                
                for( int i = 0; i < 2; i++ ) {
                    String check = modes[ i ];
                    if( check.equals( "clamp" ) ) {
                        tms[ i ] = TileMode.CLAMP;
                    }else if( check.equals( "repeat" ) ) {
                        tms[ i ] = TileMode.REPEAT;
                    }else if( check.equals( "mirror" ) ) {
                        tms[ i ] = TileMode.MIRROR;
                    }else{
                        // clear tiling, e.g. disabled
                        tms[ i ] = null;
                    }
                }
            }
            
            bm.setTileModeXY( tms[ 0 ], tms[ 1 ] );
        }
    }
    
    public static void applyContainerExtras( View container, String prefix, Theme theme, int iconSize ) {
        Drawable d = theme.getDrawable( 
                new String[] { prefix + "_button_container_background", 
                        "button_container_background" }
            );
        
        if( d != null ) {
            // resize?
            if( theme.getBoolean( new String[] { prefix + "_resize_background", "resize_background" } ) ) {
                d = resizeImage( d, 0, iconSize );
            }
            
            applyTiling( d, theme.getString( new String[] {
                    prefix + "_tile_background", 
                    "tile_background" 
                } ) );

            container.setBackgroundDrawable( d );
        }
    }
    
    public static void applyButtonExtras( ImageButton b, String prefix, String name,
                Theme theme, int iconSize ) {
        // Add our images at the size we want
        Drawable d = b.getDrawable(); 
        
        if( d == null ) {
            d = theme.getDrawable( 
                new String[] { prefix + "_button_" + name, 
                        prefix + "_button", "button_" + name, "button" }
            );
        }
        b.setImageDrawable( resizeImage( d, iconSize, iconSize ) );
        
        // add bg if not set and one is specified in the theme
        d = theme.getDrawable( 
                new String[] { prefix + "_button_background_" + name, 
                        prefix + "_button_background", "button_background_" + name,
                        "button_background" }
            );
        
        if( d != null ) {
            if( theme.getBoolean( new String[] { 
                    prefix + "_resize_button_background_" + name, 
                    prefix + "_resize_button_background", 
                    "resize_button_background_" + name,
                    "resize_button_background" } ) ) {
                d = resizeImage( d, 0, iconSize );
            }

            applyTiling( d, theme.getString( new String[] {
                prefix + "_tile_button_background_" + name, 
                prefix + "_tile_button_background", 
                "tile_button_background_" + name,
                "tile_button_background" 
            } ) );

            b.setBackgroundDrawable( d );
        }
    }
}