This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Add provider compatibility and os class port #47
Open
KeithYokoma
wants to merge
1
commit into
mixi-inc:master
Choose a base branch
from
KeithYokoma:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
...eviceCompatibility/src/main/java/jp/mixi/compatibility/android/os/ServiceManagerPort.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package jp.mixi.compatibility.android.os; | ||
|
||
import android.os.IBinder; | ||
import android.util.Log; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
|
||
/** | ||
* @author KeithYokoma | ||
*/ | ||
@SuppressWarnings("unused") | ||
public final class ServiceManagerPort { | ||
public static final String TAG = ServiceManagerPort.class.getSimpleName(); | ||
private static final String CLASS_SERVICE_MANAGER = "android.os.ServiceManager"; | ||
private static final String METHOD_GET_SERVICE = "getService"; | ||
|
||
private ServiceManagerPort() { | ||
throw new AssertionError(); | ||
} | ||
|
||
public static IBinder tryGetServiceManagerBinder(String serviceName) { | ||
try { | ||
return getServiceManagerBinder(serviceName); | ||
} catch (ClassNotFoundException e) { | ||
Log.e(TAG, "class not found: ", e); | ||
} catch (NoSuchMethodException e) { | ||
Log.e(TAG, "method not found: ", e); | ||
} catch (InvocationTargetException e) { | ||
Log.e(TAG, "invocation target error: ", e); | ||
} catch (IllegalAccessException e) { | ||
Log.e(TAG, "illegal access to the method: ", e); | ||
} | ||
return null; | ||
} | ||
|
||
public static IBinder getServiceManagerBinder(String serviceName) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { | ||
Class<?> serviceManager = ServiceManagerPort.class.getClassLoader().loadClass(CLASS_SERVICE_MANAGER); | ||
Method getService = serviceManager.getDeclaredMethod(METHOD_GET_SERVICE, String.class); | ||
if (!getService.isAccessible()) { | ||
getService.setAccessible(true); | ||
} | ||
return (IBinder) getService.invoke(null, serviceName); | ||
} | ||
} | ||
|
86 changes: 86 additions & 0 deletions
86
...viceCompatibility/src/main/java/jp/mixi/compatibility/android/provider/CallLogCompat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package jp.mixi.compatibility.android.provider; | ||
|
||
import android.annotation.SuppressLint; | ||
import android.content.ContentResolver; | ||
import android.database.Cursor; | ||
import android.os.Build; | ||
import android.provider.CallLog; | ||
import android.text.TextUtils; | ||
import android.util.Log; | ||
|
||
/** | ||
* Compatibility for the variety of call log data store. | ||
* | ||
* @author jfsso | ||
* @author KeithYokoma | ||
*/ | ||
@SuppressWarnings("unused") | ||
public final class CallLogCompat { | ||
public static final String TAG = CallLogCompat.class.getSimpleName(); | ||
public static final String COLUMN_CACHED_LOOKUP_URI = "lookup_uri"; | ||
public static final String UNKNOWN_NUMBER = "-1"; | ||
public static final String PRIVATE_NUMBER = "-2"; | ||
public static final String PAYPHONE_NUMBER = "-3"; | ||
|
||
private CallLogCompat() { | ||
throw new AssertionError(); | ||
} | ||
|
||
public static String getName(Cursor cursor, String fallback) { | ||
String name = cursor.getString(cursor.getColumnIndexOrThrow(CallLog.Calls.CACHED_NAME)); | ||
if (TextUtils.isEmpty(name)) | ||
return fallback; | ||
else | ||
return name; | ||
} | ||
|
||
@SuppressLint("InlinedApi") | ||
public static int getNumberPresentation(Cursor cursor, String number) { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { | ||
return cursor.getInt(cursor.getColumnIndex(CallLog.Calls.NUMBER_PRESENTATION)); | ||
} else { | ||
if (UNKNOWN_NUMBER.equals(number)) { | ||
return CallLog.Calls.PRESENTATION_UNKNOWN; | ||
} else if (PRIVATE_NUMBER.equals(number)) { | ||
return CallLog.Calls.PRESENTATION_RESTRICTED; | ||
} else if (PAYPHONE_NUMBER.equals(number)) { | ||
return CallLog.Calls.PRESENTATION_PAYPHONE; | ||
} | ||
} | ||
return CallLog.Calls.PRESENTATION_ALLOWED; | ||
} | ||
|
||
/** | ||
* Compatibility support method for Samsung devices to correctly fetch call logs. | ||
* Many thanks to CallLogCalendar :), and the code reference is here(https://gist.github.com/jfsso/391c4bc757bc4210f4bc). | ||
* | ||
* @param resolver the resolver, not nullable. | ||
* @param selection the selection, can be null. | ||
*/ | ||
public static String addTypeSelectionIfNeeded(ContentResolver resolver, String selection) { | ||
// Some samsung devices add all types of logs to the call log. | ||
// Here we will make an attempt to know if it is one of those. | ||
// It simply will check for the "logtype" column: | ||
// if it exists, the phone is a troublesome one: add some selection parameters | ||
// if not, the phone is a nice one ;) | ||
try { | ||
if (Build.BRAND.toLowerCase().equals("samsung")) { | ||
resolver.query(CallLog.Calls.CONTENT_URI, new String[] { "logtype" }, null, null, null); | ||
// no exception thrown | ||
// yes, it is a galaxy phone! lets add the parameter to select | ||
// only call logs | ||
Log.v(TAG, "oops, this one needs some hack for call logs X("); | ||
if(TextUtils.isEmpty(selection)) { | ||
selection = "logtype IN (100, 500)"; | ||
} else { | ||
selection += " AND logtype IN (100, 500)"; | ||
} | ||
} | ||
} catch (Exception e) { | ||
// ignore | ||
Log.v(TAG, "nice one! no need to hack call logs :)"); | ||
} | ||
Log.v(TAG, "after: " + selection); | ||
return selection; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...ceCompatibility/src/main/java/jp/mixi/compatibility/android/provider/TelephonyCompat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package jp.mixi.compatibility.android.provider; | ||
|
||
import android.annotation.SuppressLint; | ||
import android.net.Uri; | ||
import android.os.Build; | ||
import android.provider.Telephony; | ||
|
||
/** | ||
* @author jfsso | ||
* @author KeithYokoma | ||
*/ | ||
@SuppressWarnings("unused") | ||
public final class TelephonyCompat { | ||
private TelephonyCompat() { | ||
throw new AssertionError(); | ||
} | ||
|
||
public static final class Sms { | ||
public static final String TAG = Sms.class.getSimpleName(); | ||
public static final String URI_CONTENT_SMS = "content://sms"; | ||
public static final int TYPE_INBOX = 1; | ||
public static final int TYPE_SENT = 2; | ||
public static final String COLUMN_DATE = "date"; | ||
public static final String COLUMN_TYPE = "type"; | ||
public static final String COLUMN_BODY = "body"; | ||
public static final String COLUMN_ADDRESS = "address"; | ||
public static final String COLUMN_SUBJECT = "subject"; | ||
public static final String COLUMN_THREAD_ID = "thread_id"; | ||
public static final String COLUMN_PERSON = "person"; | ||
public static final String DEFAULT_SORT_ORDER = "date DESC"; | ||
|
||
private Sms() { | ||
throw new AssertionError(); | ||
} | ||
|
||
@SuppressLint("NewApi") // good to go with our compatibility | ||
public static Uri getContentUri() { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) | ||
return Telephony.Sms.CONTENT_URI; | ||
else | ||
return Uri.parse(URI_CONTENT_SMS); | ||
} | ||
|
||
@SuppressLint("NewApi") // we port from the newer api | ||
public static String getDefaultSortOrder() { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) | ||
return Telephony.Sms.DEFAULT_SORT_ORDER; | ||
else | ||
return DEFAULT_SORT_ORDER; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please follow AOSP code-style |
||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please follow AOSP code-style
https://source.android.com/source/code-style.html#use-standard-brace-style