blob: f0fc45eda87fdd3515ee3a762d0acadb77eba9e9 [file] [log] [blame]
Joe Onorato6f9d4bd2010-09-29 17:43:46 -07001#!/usr/bin/env python2.5
2
3import cgi
Winson Chung88381732013-07-12 13:41:18 -07004import codecs
Joe Onorato6f9d4bd2010-09-29 17:43:46 -07005import os
Winson Chung88381732013-07-12 13:41:18 -07006import pprint
Joe Onorato6f9d4bd2010-09-29 17:43:46 -07007import shutil
8import sys
9import sqlite3
10
Winson Chung88381732013-07-12 13:41:18 -070011SCREENS = 0
Joe Onorato6f9d4bd2010-09-29 17:43:46 -070012COLUMNS = 4
13ROWS = 4
Winson Chung88381732013-07-12 13:41:18 -070014HOTSEAT_SIZE = 4
Joe Onorato6f9d4bd2010-09-29 17:43:46 -070015CELL_SIZE = 110
16
Winson Chung88381732013-07-12 13:41:18 -070017CONTAINER_DESKTOP = -100
18CONTAINER_HOTSEAT = -101
19
Joe Onorato6f9d4bd2010-09-29 17:43:46 -070020DIR = "db_files"
21AUTO_FILE = DIR + "/launcher.db"
22INDEX_FILE = DIR + "/index.html"
23
24def usage():
Winson Chung88381732013-07-12 13:41:18 -070025 print "usage: print_db.py launcher.db <sw600|sw720> -- prints a launcher.db"
26 print "usage: print_db.py <sw600|sw720> -- adb pulls a launcher.db from a device"
Joe Onorato6f9d4bd2010-09-29 17:43:46 -070027 print " and prints it"
28 print
29 print "The dump will be created in a directory called db_files in cwd."
30 print "This script will delete any db_files directory you have now"
31
32
33def make_dir():
34 shutil.rmtree(DIR, True)
35 os.makedirs(DIR)
36
37def pull_file(fn):
38 print "pull_file: " + fn
39 rv = os.system("adb pull"
Winson Chung88381732013-07-12 13:41:18 -070040 + " /data/data/com.google.android.googlequicksearchbox/databases/launcher.db"
Joe Onorato6f9d4bd2010-09-29 17:43:46 -070041 + " " + fn);
42 if rv != 0:
43 print "adb pull failed"
44 sys.exit(1)
45
46def get_favorites(conn):
47 c = conn.cursor()
48 c.execute("SELECT * FROM favorites")
49 columns = [d[0] for d in c.description]
50 rows = []
51 for row in c:
52 rows.append(row)
53 return columns,rows
54
55def print_intent(out, id, i, cell):
56 if cell:
57 out.write("""<span class="intent" title="%s">shortcut</span>""" % (
58 cgi.escape(cell, True)
59 ))
60
61
62def print_icon(out, id, i, cell):
63 if cell:
64 icon_fn = "icon_%d.png" % id
Winson Chung88381732013-07-12 13:41:18 -070065 out.write("""<img style="width: 3em; height: 3em;" src="%s">""" % ( icon_fn ))
Joe Onorato6f9d4bd2010-09-29 17:43:46 -070066 f = file(DIR + "/" + icon_fn, "w")
67 f.write(cell)
68 f.close()
69
Winson Chung88381732013-07-12 13:41:18 -070070def print_icon_type(out, id, i, cell):
71 if cell == 0:
72 out.write("Application (%d)" % cell)
73 elif cell == 1:
74 out.write("Shortcut (%d)" % cell)
75 elif cell == 2:
76 out.write("Folder (%d)" % cell)
77 elif cell == 4:
78 out.write("Widget (%d)" % cell)
79 elif cell:
80 out.write("%d" % cell)
81
Joe Onorato6f9d4bd2010-09-29 17:43:46 -070082def print_cell(out, id, i, cell):
83 if not cell is None:
Winson Chung88381732013-07-12 13:41:18 -070084 out.write(cgi.escape(unicode(cell)))
Joe Onorato6f9d4bd2010-09-29 17:43:46 -070085
86FUNCTIONS = {
87 "intent": print_intent,
Winson Chung88381732013-07-12 13:41:18 -070088 "icon": print_icon,
89 "iconType": print_icon_type
Joe Onorato6f9d4bd2010-09-29 17:43:46 -070090}
91
Winson Chungbfc003a2011-08-24 11:32:02 -070092def render_cell_info(out, cell, occupied):
93 if cell is None:
94 out.write(" <td width=%d height=%d></td>\n" %
95 (CELL_SIZE, CELL_SIZE))
96 elif cell == occupied:
97 pass
98 else:
99 cellX = cell["cellX"]
100 cellY = cell["cellY"]
101 spanX = cell["spanX"]
102 spanY = cell["spanY"]
103 intent = cell["intent"]
104 if intent:
105 title = "title=\"%s\"" % cgi.escape(cell["intent"], True)
106 else:
107 title = ""
108 out.write((" <td colspan=%d rowspan=%d width=%d height=%d"
109 + " bgcolor=#dddddd align=center valign=middle %s>") % (
110 spanX, spanY,
111 (CELL_SIZE*spanX), (CELL_SIZE*spanY),
112 title))
113 itemType = cell["itemType"]
114 if itemType == 0:
Winson Chung88381732013-07-12 13:41:18 -0700115 out.write("""<img style="width: 4em; height: 4em;" src="icon_%d.png">\n""" % ( cell["_id"] ))
Winson Chungbfc003a2011-08-24 11:32:02 -0700116 out.write("<br/>\n")
117 out.write(cgi.escape(cell["title"]) + " <br/><i>(app)</i>")
118 elif itemType == 1:
Winson Chung88381732013-07-12 13:41:18 -0700119 out.write("""<img style="width: 4em; height: 4em;" src="icon_%d.png">\n""" % ( cell["_id"] ))
Winson Chungbfc003a2011-08-24 11:32:02 -0700120 out.write("<br/>\n")
121 out.write(cgi.escape(cell["title"]) + " <br/><i>(shortcut)</i>")
122 elif itemType == 2:
123 out.write("""<i>folder</i>""")
Winson Chungbfc003a2011-08-24 11:32:02 -0700124 elif itemType == 4:
125 out.write("<i>widget %d</i><br/>\n" % cell["appWidgetId"])
Winson Chungbfc003a2011-08-24 11:32:02 -0700126 else:
127 out.write("<b>unknown type: %d</b>" % itemType)
128 out.write("</td>\n")
129
Joe Onorato6f9d4bd2010-09-29 17:43:46 -0700130def process_file(fn):
Winson Chung88381732013-07-12 13:41:18 -0700131 global SCREENS, COLUMNS, ROWS, HOTSEAT_SIZE
Joe Onorato6f9d4bd2010-09-29 17:43:46 -0700132 print "process_file: " + fn
133 conn = sqlite3.connect(fn)
134 columns,rows = get_favorites(conn)
Winson Chung88381732013-07-12 13:41:18 -0700135
Joe Onorato6f9d4bd2010-09-29 17:43:46 -0700136 data = [dict(zip(columns,row)) for row in rows]
137
Winson Chung88381732013-07-12 13:41:18 -0700138 # Calculate the proper number of screens, columns, and rows in this db
139 screensIdMap = []
140 hotseatIdMap = []
141 HOTSEAT_SIZE = 0
142 for d in data:
143 if d["container"] == CONTAINER_DESKTOP:
144 if d["screen"] not in screensIdMap:
145 screensIdMap.append(d["screen"])
146 COLUMNS = max(COLUMNS, d["cellX"] + d["spanX"])
147 ROWS = max(ROWS, d["cellX"] + d["spanX"])
148 elif d["container"] == CONTAINER_HOTSEAT:
149 hotseatIdMap.append(d["screen"])
150 HOTSEAT_SIZE = max(HOTSEAT_SIZE, d["screen"] + 1)
151 SCREENS = len(screensIdMap)
152
153 out = codecs.open(INDEX_FILE, encoding="utf-8", mode="w")
Joe Onorato6f9d4bd2010-09-29 17:43:46 -0700154 out.write("""<html>
155<head>
156<style type="text/css">
157.intent {
158 font-style: italic;
159}
160</style>
161</head>
162<body>
163""")
164
165 # Data table
166 out.write("<b>Favorites table</b><br/>\n")
167 out.write("""<html>
168<table border=1 cellspacing=0 cellpadding=4>
169<tr>
170""")
171 print_functions = []
172 for col in columns:
173 print_functions.append(FUNCTIONS.get(col, print_cell))
174 for i in range(0,len(columns)):
175 col = columns[i]
176 out.write(""" <th>%s</th>
177""" % ( col ))
178 out.write("""
179</tr>
180""")
Winson Chung88381732013-07-12 13:41:18 -0700181
Joe Onorato6f9d4bd2010-09-29 17:43:46 -0700182 for row in rows:
183 out.write("""<tr>
184""")
185 for i in range(0,len(row)):
186 cell = row[i]
187 # row[0] is always _id
188 out.write(""" <td>""")
189 print_functions[i](out, row[0], row, cell)
190 out.write("""</td>
191""")
192 out.write("""</tr>
193""")
194 out.write("""</table>
195""")
196
Winson Chungbfc003a2011-08-24 11:32:02 -0700197 # Hotseat
198 hotseat = []
199 for i in range(0, HOTSEAT_SIZE):
200 hotseat.append(None)
201 for row in data:
Winson Chung88381732013-07-12 13:41:18 -0700202 if row["container"] != CONTAINER_HOTSEAT:
Winson Chungbfc003a2011-08-24 11:32:02 -0700203 continue
204 screen = row["screen"]
205 hotseat[screen] = row
206 out.write("<br/><b>Hotseat</b><br/>\n")
207 out.write("<table class=layout border=1 cellspacing=0 cellpadding=4>\n")
208 for cell in hotseat:
209 render_cell_info(out, cell, None)
210 out.write("</table>\n")
211
Joe Onorato6f9d4bd2010-09-29 17:43:46 -0700212 # Pages
213 screens = []
214 for i in range(0,SCREENS):
215 screen = []
216 for j in range(0,ROWS):
217 m = []
218 for k in range(0,COLUMNS):
219 m.append(None)
220 screen.append(m)
221 screens.append(screen)
222 occupied = "occupied"
223 for row in data:
Joe Onorato6f9d4bd2010-09-29 17:43:46 -0700224 # desktop
Winson Chung88381732013-07-12 13:41:18 -0700225 if row["container"] != CONTAINER_DESKTOP:
Joe Onorato6f9d4bd2010-09-29 17:43:46 -0700226 continue
Winson Chung88381732013-07-12 13:41:18 -0700227 screen = screens[screensIdMap.index(row["screen"])]
Joe Onorato6f9d4bd2010-09-29 17:43:46 -0700228 cellX = row["cellX"]
229 cellY = row["cellY"]
230 spanX = row["spanX"]
231 spanY = row["spanY"]
232 for j in range(cellY, cellY+spanY):
233 for k in range(cellX, cellX+spanX):
234 screen[j][k] = occupied
235 screen[cellY][cellX] = row
236 i=0
237 for screen in screens:
238 out.write("<br/><b>Screen %d</b><br/>\n" % i)
239 out.write("<table class=layout border=1 cellspacing=0 cellpadding=4>\n")
240 for m in screen:
241 out.write(" <tr>\n")
242 for cell in m:
Winson Chungbfc003a2011-08-24 11:32:02 -0700243 render_cell_info(out, cell, occupied)
Joe Onorato6f9d4bd2010-09-29 17:43:46 -0700244 out.write("</tr>\n")
245 out.write("</table>\n")
246 i=i+1
247
248 out.write("""
249</body>
250</html>
251""")
252
253 out.close()
254
Winson Chung88381732013-07-12 13:41:18 -0700255def updateDeviceClassConstants(str):
256 global SCREENS, COLUMNS, ROWS, HOTSEAT_SIZE
257 devClass = str.lower()
258 if devClass == "sw600":
259 COLUMNS = 6
260 ROWS = 6
261 HOTSEAT_SIZE = 6
262 return True
263 elif devClass == "sw720":
264 COLUMNS = 8
265 ROWS = 6
266 HOTSEAT_SIZE = 8
267 return True
268 return False
269
Joe Onorato6f9d4bd2010-09-29 17:43:46 -0700270def main(argv):
Winson Chung88381732013-07-12 13:41:18 -0700271 if len(argv) == 1 or (len(argv) == 2 and updateDeviceClassConstants(argv[1])):
Joe Onorato6f9d4bd2010-09-29 17:43:46 -0700272 make_dir()
273 pull_file(AUTO_FILE)
274 process_file(AUTO_FILE)
Winson Chung88381732013-07-12 13:41:18 -0700275 elif len(argv) == 2 or (len(argv) == 3 and updateDeviceClassConstants(argv[2])):
Joe Onorato6f9d4bd2010-09-29 17:43:46 -0700276 make_dir()
277 process_file(argv[1])
278 else:
279 usage()
280
281if __name__=="__main__":
282 main(sys.argv)