~w-shackleton/droidpad-android/stable

« back to all changes in this revision

Viewing changes to src/uk/digitalsquid/droidpad/layout/Scanner.java

  • Committer: William Shackleton
  • Date: 2013-04-12 14:42:12 UTC
  • mfrom: (51.3.5 json)
  • mto: This revision was merged to the branch mainline in revision 52.
  • Revision ID: w.shackleton@gmail.com-20130412144212-4f7pueqwee0lc3yd
Imported code to load JSON layouts

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package uk.digitalsquid.droidpad.xml;
 
1
/*  This file is part of DroidPad.
 
2
 *
 
3
 *  DroidPad is free software: you can redistribute it and/or modify
 
4
 *  it under the terms of the GNU General Public License as published by
 
5
 *  the Free Software Foundation, either version 3 of the License, or
 
6
 *  (at your option) any later version.
 
7
 *
 
8
 *  DroidPad is distributed in the hope that it will be useful,
 
9
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 *  GNU General Public License for more details.
 
12
 *
 
13
 *  You should have received a copy of the GNU General Public License
 
14
 *  along with DroidPad.  If not, see <http://www.gnu.org/licenses/>.
 
15
 */
 
16
 
 
17
package uk.digitalsquid.droidpad.layout;
2
18
 
 
19
import java.io.BufferedReader;
3
20
import java.io.File;
4
 
import java.io.FileInputStream;
 
21
import java.io.FileReader;
5
22
import java.io.IOException;
6
23
import java.util.ArrayList;
7
24
import java.util.List;
56
73
                
57
74
                for(File layout : layouts) {
58
75
                        try {
59
 
                                ModeSpec spec = LayoutDecoder.decodeLayout(new FileInputStream(layout));
 
76
                                ModeSpec spec = decodeLayout(layout);
60
77
                                switch(spec.getMode()) {
61
78
                                case ModeSpec.LAYOUTS_JS:
62
79
                                        jsModes.add(spec.getLayout());
75
92
                                Toast.makeText(context, "Failed to load custom layout " + layout.getName(), Toast.LENGTH_SHORT).show();
76
93
                        }
77
94
                }
 
95
        }
 
96
        
 
97
        static final ModeSpec decodeLayout(File layout) throws IOException {
 
98
                BufferedReader reader = new BufferedReader(new FileReader(layout));
 
99
                reader.mark(256);
 
100
                char[] head = new char[1];
 
101
                reader.read(head);
 
102
                reader.reset();
 
103
                switch(head[0]) {
 
104
                case '<': // XML
 
105
                        return XmlDecoder.decodeLayout(reader);
 
106
                case '{': // JSON
 
107
                        return JsonDecoder.decodeLayout(reader);
 
108
                default: // Not sure; try both
 
109
                        try {
 
110
                                return XmlDecoder.decodeLayout(reader);
 
111
                        } catch(IOException e) {
 
112
                                Log.w(TAG, "File " + layout.getAbsolutePath() + " couldn't be decoded as XML");
 
113
                        }
 
114
                        return JsonDecoder.decodeLayout(reader);
 
115
                }
78
116
        }
79
117
        
80
118
        private boolean isExternalStorageReadable() {