| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <jni.h> | 5 #include <jni.h> |
| 6 #include <sys/system_properties.h> | 6 #include <sys/system_properties.h> |
| 7 | 7 |
| 8 #include "sync/util/session_utils_android.h" | 8 #include "sync/util/session_utils_android.h" |
| 9 | 9 |
| 10 #include "base/android/jni_android.h" | 10 #include "base/android/jni_android.h" |
| 11 #include "base/android/jni_string.h" | 11 #include "base/android/jni_string.h" |
| 12 #include "base/android/scoped_java_ref.h" | 12 #include "base/android/scoped_java_ref.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 | 14 |
| 15 using base::android::AttachCurrentThread; | 15 using base::android::AttachCurrentThread; |
| 16 using base::android::CheckException; | 16 using base::android::CheckException; |
| 17 using base::android::ConvertUTF8ToJavaString; | 17 using base::android::ConvertUTF8ToJavaString; |
| 18 using base::android::GetApplicationContext; | 18 using base::android::GetApplicationContext; |
| 19 using base::android::GetClass; | 19 using base::android::GetClass; |
| 20 using base::android::MethodID; |
| 20 using base::android::JavaRef; | 21 using base::android::JavaRef; |
| 21 using base::android::ScopedJavaLocalRef; | 22 using base::android::ScopedJavaLocalRef; |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 ScopedJavaLocalRef<jstring> GetAndroidIdJNI( | 26 ScopedJavaLocalRef<jstring> GetAndroidIdJNI( |
| 26 JNIEnv* env, const JavaRef<jobject>& content_resolver) { | 27 JNIEnv* env, const JavaRef<jobject>& content_resolver) { |
| 27 ScopedJavaLocalRef<jclass> clazz( | 28 ScopedJavaLocalRef<jclass> clazz( |
| 28 GetClass(env, "android/provider/Settings$Secure")); | 29 GetClass(env, "android/provider/Settings$Secure")); |
| 29 jmethodID j_get_string = GetStaticMethodID(env, clazz, "getString", | 30 jmethodID j_get_string = MethodID::Get< |
| 30 "(Landroid/content/ContentResolver;Ljava/lang/String;)" | 31 MethodID::METHODTYPE_STATIC, MethodID::EXCEPTIONCHECK_YES>( |
| 32 env, clazz.obj(), "getString", |
| 33 "(Landroid/content/ContentResolver;Ljava/lang/String;)" |
| 31 "Ljava/lang/String;"); | 34 "Ljava/lang/String;"); |
| 32 ScopedJavaLocalRef<jstring> j_android_id = | 35 ScopedJavaLocalRef<jstring> j_android_id = |
| 33 ConvertUTF8ToJavaString(env, "android_id"); | 36 ConvertUTF8ToJavaString(env, "android_id"); |
| 34 jstring android_id = static_cast<jstring>( | 37 jstring android_id = static_cast<jstring>( |
| 35 env->CallStaticObjectMethod( | 38 env->CallStaticObjectMethod( |
| 36 clazz.obj(), j_get_string, content_resolver.obj(), | 39 clazz.obj(), j_get_string, content_resolver.obj(), |
| 37 j_android_id.obj())); | 40 j_android_id.obj())); |
| 38 CheckException(env); | 41 CheckException(env); |
| 39 return ScopedJavaLocalRef<jstring>(env, android_id); | 42 return ScopedJavaLocalRef<jstring>(env, android_id); |
| 40 } | 43 } |
| 41 | 44 |
| 42 ScopedJavaLocalRef<jobject> GetContentResolver(JNIEnv* env) { | 45 ScopedJavaLocalRef<jobject> GetContentResolver(JNIEnv* env) { |
| 43 ScopedJavaLocalRef<jclass> clazz(GetClass(env, "android/content/Context")); | 46 ScopedJavaLocalRef<jclass> clazz(GetClass(env, "android/content/Context")); |
| 44 jmethodID j_get_content_resolver_method = GetMethodID( | 47 jmethodID j_get_content_resolver_method = MethodID::Get< |
| 45 env, clazz,"getContentResolver", "()Landroid/content/ContentResolver;"); | 48 MethodID::METHODTYPE_NORMAL, MethodID::EXCEPTIONCHECK_YES>( |
| 49 env, clazz.obj(), "getContentResolver", |
| 50 "()Landroid/content/ContentResolver;"); |
| 46 jobject content_resolver = env->CallObjectMethod( | 51 jobject content_resolver = env->CallObjectMethod( |
| 47 GetApplicationContext(), j_get_content_resolver_method); | 52 GetApplicationContext(), j_get_content_resolver_method); |
| 48 CheckException(env); | 53 CheckException(env); |
| 49 return ScopedJavaLocalRef<jobject>(env, content_resolver); | 54 return ScopedJavaLocalRef<jobject>(env, content_resolver); |
| 50 } | 55 } |
| 51 | 56 |
| 52 } | 57 } |
| 53 | 58 |
| 54 namespace syncer { | 59 namespace syncer { |
| 55 namespace internal { | 60 namespace internal { |
| 56 | 61 |
| 57 std::string GetAndroidId() { | 62 std::string GetAndroidId() { |
| 58 JNIEnv* env = AttachCurrentThread(); | 63 JNIEnv* env = AttachCurrentThread(); |
| 59 ScopedJavaLocalRef<jstring> android_id = | 64 ScopedJavaLocalRef<jstring> android_id = |
| 60 GetAndroidIdJNI(env, GetContentResolver(env)); | 65 GetAndroidIdJNI(env, GetContentResolver(env)); |
| 61 return ConvertJavaStringToUTF8(android_id); | 66 return ConvertJavaStringToUTF8(android_id); |
| 62 } | 67 } |
| 63 | 68 |
| 64 std::string GetModel() { | 69 std::string GetModel() { |
| 65 char model[PROP_VALUE_MAX]; | 70 char model[PROP_VALUE_MAX]; |
| 66 __system_property_get("ro.product.model", model); | 71 __system_property_get("ro.product.model", model); |
| 67 return model; | 72 return model; |
| 68 } | 73 } |
| 69 | 74 |
| 70 } // namespace internal | 75 } // namespace internal |
| 71 } // namespace syncer | 76 } // namespace syncer |
| OLD | NEW |