|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 package org.mozilla.gecko; |
|
6 |
|
7 import java.io.File; |
|
8 import java.util.Map; |
|
9 import java.util.Map.Entry; |
|
10 |
|
11 import org.mozilla.gecko.GeckoProfileDirectories.NoMozillaDirectoryException; |
|
12 import org.mozilla.gecko.db.BrowserContract; |
|
13 |
|
14 import android.content.ContentProvider; |
|
15 import android.content.ContentValues; |
|
16 import android.content.UriMatcher; |
|
17 import android.database.Cursor; |
|
18 import android.database.MatrixCursor; |
|
19 import android.net.Uri; |
|
20 import android.util.Log; |
|
21 |
|
22 /** |
|
23 * This is not a per-profile provider. This provider allows read-only, |
|
24 * restricted access to certain attributes of Fennec profiles. |
|
25 */ |
|
26 public class GeckoProfilesProvider extends ContentProvider { |
|
27 private static final String LOG_TAG = "GeckoProfilesProvider"; |
|
28 |
|
29 private static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH); |
|
30 |
|
31 private static final int PROFILES = 100; |
|
32 private static final int PROFILES_NAME = 101; |
|
33 private static final int PROFILES_DEFAULT = 200; |
|
34 |
|
35 private static final String[] DEFAULT_ARGS = { |
|
36 BrowserContract.Profiles.NAME, |
|
37 BrowserContract.Profiles.PATH, |
|
38 }; |
|
39 |
|
40 static { |
|
41 URI_MATCHER.addURI(BrowserContract.PROFILES_AUTHORITY, "profiles", PROFILES); |
|
42 URI_MATCHER.addURI(BrowserContract.PROFILES_AUTHORITY, "profiles/*", PROFILES_NAME); |
|
43 URI_MATCHER.addURI(BrowserContract.PROFILES_AUTHORITY, "default", PROFILES_DEFAULT); |
|
44 } |
|
45 |
|
46 @Override |
|
47 public String getType(Uri uri) { |
|
48 return null; |
|
49 } |
|
50 |
|
51 @Override |
|
52 public boolean onCreate() { |
|
53 // Successfully loaded. |
|
54 return true; |
|
55 } |
|
56 |
|
57 private String[] profileValues(final String name, final String path, int len, int nameIndex, int pathIndex) { |
|
58 final String[] values = new String[len]; |
|
59 if (nameIndex >= 0) { |
|
60 values[nameIndex] = name; |
|
61 } |
|
62 if (pathIndex >= 0) { |
|
63 values[pathIndex] = path; |
|
64 } |
|
65 return values; |
|
66 } |
|
67 |
|
68 protected void addRowForProfile(final MatrixCursor cursor, final int len, final int nameIndex, final int pathIndex, final String name, final String path) { |
|
69 if (path == null || name == null) { |
|
70 return; |
|
71 } |
|
72 |
|
73 String[] values = new String[len]; |
|
74 if (nameIndex >= 0) { |
|
75 values[nameIndex] = name; |
|
76 } |
|
77 if (pathIndex >= 0) { |
|
78 values[pathIndex] = path; |
|
79 } |
|
80 cursor.addRow(profileValues(name, path, len, nameIndex, pathIndex)); |
|
81 } |
|
82 |
|
83 protected Cursor getCursorForProfiles(final String[] args, Map<String, String> profiles) { |
|
84 // Compute the projection. |
|
85 int nameIndex = -1; |
|
86 int pathIndex = -1; |
|
87 for (int i = 0; i < args.length; ++i) { |
|
88 if (BrowserContract.Profiles.NAME.equals(args[i])) { |
|
89 nameIndex = i; |
|
90 } else if (BrowserContract.Profiles.PATH.equals(args[i])) { |
|
91 pathIndex = i; |
|
92 } |
|
93 } |
|
94 |
|
95 final MatrixCursor cursor = new MatrixCursor(args); |
|
96 for (Entry<String, String> entry : profiles.entrySet()) { |
|
97 addRowForProfile(cursor, args.length, nameIndex, pathIndex, entry.getKey(), entry.getValue()); |
|
98 } |
|
99 return cursor; |
|
100 } |
|
101 |
|
102 @Override |
|
103 public Cursor query(Uri uri, String[] projection, String selection, |
|
104 String[] selectionArgs, String sortOrder) { |
|
105 |
|
106 final String[] args = (projection == null) ? DEFAULT_ARGS : projection; |
|
107 |
|
108 final File mozillaDir; |
|
109 try { |
|
110 mozillaDir = GeckoProfileDirectories.getMozillaDirectory(getContext()); |
|
111 } catch (NoMozillaDirectoryException e) { |
|
112 Log.d(LOG_TAG, "No Mozilla directory; cannot query for profiles. Assuming there are none."); |
|
113 return new MatrixCursor(projection); |
|
114 } |
|
115 |
|
116 final Map<String, String> matchingProfiles; |
|
117 |
|
118 final int match = URI_MATCHER.match(uri); |
|
119 switch (match) { |
|
120 case PROFILES: |
|
121 // Return all profiles. |
|
122 matchingProfiles = GeckoProfileDirectories.getAllProfiles(mozillaDir); |
|
123 break; |
|
124 case PROFILES_NAME: |
|
125 // Return data about the specified profile. |
|
126 final String name = uri.getLastPathSegment(); |
|
127 matchingProfiles = GeckoProfileDirectories.getProfilesNamed(mozillaDir, |
|
128 name); |
|
129 break; |
|
130 case PROFILES_DEFAULT: |
|
131 matchingProfiles = GeckoProfileDirectories.getDefaultProfile(mozillaDir); |
|
132 break; |
|
133 default: |
|
134 throw new UnsupportedOperationException("Unknown query URI " + uri); |
|
135 } |
|
136 |
|
137 return getCursorForProfiles(args, matchingProfiles); |
|
138 } |
|
139 |
|
140 @Override |
|
141 public Uri insert(Uri uri, ContentValues values) { |
|
142 throw new IllegalStateException("Inserts not supported."); |
|
143 } |
|
144 |
|
145 @Override |
|
146 public int delete(Uri uri, String selection, String[] selectionArgs) { |
|
147 throw new IllegalStateException("Deletes not supported."); |
|
148 } |
|
149 |
|
150 @Override |
|
151 public int update(Uri uri, ContentValues values, String selection, |
|
152 String[] selectionArgs) { |
|
153 throw new IllegalStateException("Updates not supported."); |
|
154 } |
|
155 |
|
156 } |