0
0
Fork 0
mirror of https://github.com/YTVanced/VancedMicroG synced 2024-11-24 20:25:14 +00:00

Add prefixing to Gservices

This commit is contained in:
mar-v-in 2015-02-25 23:14:39 +01:00
parent 6b2493a6d1
commit 81b660f1a7
3 changed files with 99 additions and 13 deletions

View file

@ -22,6 +22,9 @@ import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteOpenHelper;
import java.util.HashMap;
import java.util.Map;
public class DatabaseHelper extends SQLiteOpenHelper { public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DB_VERSION = 3; private static final int DB_VERSION = 3;
private static final int DB_VERSION_OLD = 1; private static final int DB_VERSION_OLD = 1;
@ -70,6 +73,29 @@ public class DatabaseHelper extends SQLiteOpenHelper {
return result; return result;
} }
public Map<String, String> search(String search) {
Map<String, String> map = new HashMap<>();
Cursor cursor = getReadableDatabase().query("overrides", new String[]{"name", "value"},
"name LIKE ?", new String[]{search}, null, null, null, null);
if (cursor != null) {
while (cursor.moveToNext()) {
map.put(cursor.getString(0), cursor.getString(1));
}
cursor.close();
}
cursor = getReadableDatabase().query("main", new String[]{"name", "value"},
"name LIKE ?", new String[]{search}, null, null, null, null);
if (cursor != null) {
if (cursor.moveToNext()) {
if (!map.containsKey(cursor.getString(0)))
map.put(cursor.getString(0), cursor.getString(1));
}
cursor.close();
}
return map;
}
public void put(String table, ContentValues values) { public void put(String table, ContentValues values) {
getWritableDatabase().insertWithOnConflict(table, null, values, SQLiteDatabase.CONFLICT_REPLACE); getWritableDatabase().insertWithOnConflict(table, null, values, SQLiteDatabase.CONFLICT_REPLACE);
} }

View file

@ -0,0 +1,34 @@
/*
* Copyright 2013-2015 µg Project Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.microg.gms.gservices;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;
public class GServices {
public static final Uri CONTENT_URI = Uri.parse("content://com.google.android.gsf.gservices");
public static final Uri MAIN_URI = Uri.parse("content://com.google.android.gsf.gservices/main");
public static final Uri OVERRIDE_URI = Uri.parse("content://com.google.android.gsf.gservices/override");
public static void setString(ContentResolver resolver, String key, String value) {
ContentValues values = new ContentValues();
values.put("name", key);
values.put("value", value);
resolver.update(MAIN_URI, values, null, null);
}
}

View file

@ -25,7 +25,10 @@ import android.os.Build;
import android.util.Log; import android.util.Log;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Set;
/** /**
* Originally found in Google Services Framework (com.google.android.gsf), this provides a generic * Originally found in Google Services Framework (com.google.android.gsf), this provides a generic
@ -35,13 +38,16 @@ import java.util.Map;
* if certain "experiments" are enabled on the device. * if certain "experiments" are enabled on the device.
*/ */
public class GServicesProvider extends ContentProvider { public class GServicesProvider extends ContentProvider {
public static final Uri MAIN_URI = Uri.parse("content://com.google.android.gsf.gservices/main"); public static final Uri CONTENT_URI = Uri.parse("content://com.google.android.gsf.gservices/");
public static final Uri OVERRIDE_URI = Uri.parse("content://com.google.android.gsf.gservices/override"); public static final Uri MAIN_URI = Uri.withAppendedPath(CONTENT_URI, "main");
public static final Uri OVERRIDE_URI = Uri.withAppendedPath(CONTENT_URI, "override");
public static final Uri PREFIX_URI = Uri.withAppendedPath(CONTENT_URI, "prefix");
private static final String TAG = "GmsServicesProvider"; private static final String TAG = "GmsServicesProvider";
private DatabaseHelper databaseHelper; private DatabaseHelper databaseHelper;
private Map<String, String> cache = new HashMap<>(); private Map<String, String> cache = new HashMap<>();
private Set<String> cachedPrefixes = new HashSet<>();
@Override @Override
public boolean onCreate() { public boolean onCreate() {
@ -60,6 +66,22 @@ public class GServicesProvider extends ContentProvider {
@Override @Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
MatrixCursor cursor = new MatrixCursor(new String[]{"name", "value"}); MatrixCursor cursor = new MatrixCursor(new String[]{"name", "value"});
if (PREFIX_URI.equals(uri)) {
for (String prefix : selectionArgs) {
if (!cachedPrefixes.contains(prefix)) {
cache.putAll(databaseHelper.search(prefix + "%"));
cachedPrefixes.add(prefix);
}
for (String name : cache.keySet()) {
if (name.startsWith(prefix)) {
String value = cache.get(name);
Log.d(TAG, "query caller=" + getCallingPackageName() + " name=" + name + " value=" + value);
cursor.addRow(new String[]{name, value});
}
}
}
} else {
for (String name : selectionArgs) { for (String name : selectionArgs) {
String value; String value;
if (cache.containsKey(name)) { if (cache.containsKey(name)) {
@ -73,6 +95,7 @@ public class GServicesProvider extends ContentProvider {
cursor.addRow(new String[]{name, value}); cursor.addRow(new String[]{name, value});
} }
} }
}
return cursor; return cursor;
} }
@ -100,7 +123,10 @@ public class GServicesProvider extends ContentProvider {
} else if (uri.equals(OVERRIDE_URI)) { } else if (uri.equals(OVERRIDE_URI)) {
databaseHelper.put("override", values); databaseHelper.put("override", values);
} }
cache.remove(values.getAsString("name")); String name = values.getAsString("name");
cache.remove(name);
Iterator<String> iterator = cachedPrefixes.iterator();
while (iterator.hasNext()) if (name.startsWith(iterator.next())) iterator.remove();
return 1; return 1;
} }
} }