Android Native新增JNI介面例項
阿新 • • 發佈:2019-02-03
diff --git a/frameworks/av/media/mtp/MtpServer.cpp b/frameworks/av/media/mtp/MtpServer.cpp
index 31c3ecf..a0b4b77 100644
--- a/frameworks/av/media/mtp/MtpServer.cpp
+++ b/frameworks/av/media/mtp/MtpServer.cpp
@@ -50,6 +50,9 @@
namespace android {
+static int objAddCount;
+static bool showToast = false;
+
static const MtpOperationCode kSupportedOperationCodes[] = {
MTP_OPERATION_GET_DEVICE_INFO,
MTP_OPERATION_OPEN_SESSION,
@@ -392,7 +395,12 @@ void MtpServer::run() {
mFD = -1;
}
+bool MtpServer::showMtpToast() {
+ return showToast;
+}
+
void MtpServer::sendObjectAdded(MtpObjectHandle handle) {
+ objAddCount = handle;</span>
ALOGI("sendObjectAdded %d\n", handle);
sendEvent(MTP_EVENT_OBJECT_ADDED, handle);
}
@@ -441,6 +449,8 @@ void MtpServer::sendEvent(MtpEventCode code, uint32_t param1) {
sxlog_printf(ANDROID_LOG_ERROR, "MtpServer",
"mEvent.write returned %d, errno: %d\n", ret, errno);
+ if (ETIME == errno && objAddCount > 0) showToast = true;
+
if(code == MTP_EVENT_STORE_ADDED || code == MTP_EVENT_STORE_REMOVED)
{
//File under Transfer, get or send, backup the event and send out after file transfer
@@ -451,6 +461,8 @@ void MtpServer::sendEvent(MtpEventCode code, uint32_t param1) {
mBackupStorageId = param1;
}
}
+ } else {
+ showToast = false;
}
//Added Modification for ALPS00276320
}
diff --git a/frameworks/av/media/mtp/MtpServer.h b/frameworks/av/media/mtp/MtpServer.h
index fcf6a81..2a90e93 100644
--- a/frameworks/av/media/mtp/MtpServer.h
+++ b/frameworks/av/media/mtp/MtpServer.h
@@ -115,6 +115,7 @@ public:
void run();
+ bool showMtpToast();
void sendObjectAdded(MtpObjectHandle handle);
void sendObjectRemoved(MtpObjectHandle handle);
//ALPS00289309, update Object
diff --git a/frameworks/base/media/java/android/mtp/MtpServer.java b/frameworks/base/media/java/android/mtp/MtpServer.java
index 678d141..3ef987d 100644
--- a/frameworks/base/media/java/android/mtp/MtpServer.java
+++ b/frameworks/base/media/java/android/mtp/MtpServer.java
@@ -70,6 +70,10 @@ public class MtpServer implements Runnable {
/// M: @}
}
+ public boolean showMtpToast() {
+ return native_show_mtp_toast();
+ }
+
public void sendObjectAdded(int handle) {
native_send_object_added(handle);
}
@@ -139,6 +143,7 @@ public class MtpServer implements Runnable {
private native final void native_setup(MtpDatabase database, boolean usePtp);
private native final void native_run();
private native final void native_cleanup();
+ private native final boolean native_show_mtp_toast();</span>
private native final void native_send_object_added(int handle);
private native final void native_send_object_removed(int handle);
private native final void native_add_storage(MtpStorage storage);
diff --git a/frameworks/base/media/jni/android_mtp_MtpServer.cpp b/frameworks/base/media/jni/android_mtp_MtpServer.cpp
index debfa11..179c2ec 100644
--- a/frameworks/base/media/jni/android_mtp_MtpServer.cpp
+++ b/frameworks/base/media/jni/android_mtp_MtpServer.cpp
@@ -112,6 +112,20 @@ android_mtp_MtpServer_cleanup(JNIEnv *env, jobject thiz)
}
}
+static bool
+android_mtp_MtpServer_show_mtp_toast(JNIEnv *env, jobject thiz, jint handle)
+{
+ Mutex::Autolock autoLock(sMutex);
+
+ MtpServer* server = getMtpServer(env, thiz);
+ if (server)
+ return server->showMtpToast();
+ else
+ ALOGE("server is null in show_mtp_toast");
+
+ return false;
+}
+
static void
android_mtp_MtpServer_send_object_added(JNIEnv *env, jobject thiz, jint handle)
{
@@ -287,6 +301,7 @@ static JNINativeMethod gMethods[] = {
(void *)android_mtp_MtpServer_setup},
{"native_run", "()V", (void *)android_mtp_MtpServer_run},
{"native_cleanup", "()V", (void *)android_mtp_MtpServer_cleanup},
+ {"native_show_mtp_toast", "()Z", (void *)android_mtp_MtpServer_show_mtp_toast},</span>
{"native_send_object_added", "(I)V", (void *)android_mtp_MtpServer_send_object_added},
{"native_send_object_removed", "(I)V", (void *)android_mtp_MtpServer_send_object_removed},
{"native_add_storage", "(Landroid/mtp/MtpStorage;)V",
diff --git a/packages/providers/MediaProvider/res/values-zh-rCN/strings.xml b/packages/providers/MediaProvider/res/values-zh-rCN/strings.xml
index 9bf46c9..48f45a4 100644
--- a/packages/providers/MediaProvider/res/values-zh-rCN/strings.xml
+++ b/packages/providers/MediaProvider/res/values-zh-rCN/strings.xml
@@ -26,4 +26,5 @@
<string name="root_images" msgid="7098113056247445324">"圖片"</string>
<string name="root_videos" msgid="3304457332406057833">"視訊"</string>
<string name="root_audio" msgid="7177565505195715659">"音訊"</string>
+ <string name="toast_text" msgid="7188565505195715659">"MTP錯誤,請重啟裝置。"</string>
</resources>
diff --git a/packages/providers/MediaProvider/res/values/strings.xml b/packages/providers/MediaProvider/res/values/strings.xml
index 8e8c541..8d09925 100644
--- a/packages/providers/MediaProvider/res/values/strings.xml
+++ b/packages/providers/MediaProvider/res/values/strings.xml
@@ -43,5 +43,6 @@
<string name="root_videos">Videos</string>
<!-- Title for documents backend that offers audio. [CHAR LIMIT=24] -->
<string name="root_audio">Audio</string>
+ <string name="toast_text">MTP error, please reboot vinci.</string>
</resources>
diff --git a/packages/providers/MediaProvider/src/com/android/providers/media/MtpService.java b/packages/providers/MediaProvider/src/com/android/providers/media/MtpService.java
index a6edf48..a16b53c 100644
--- a/packages/providers/MediaProvider/src/com/android/providers/media/MtpService.java
+++ b/packages/providers/MediaProvider/src/com/android/providers/media/MtpService.java
@@ -19,6 +19,7 @@ package com.android.providers.media;
import android.app.ActivityManager;
import android.app.KeyguardManager;
import android.app.Service;
+import android.widget.Toast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
@@ -29,6 +30,7 @@ import android.mtp.MtpServer;
import android.mtp.MtpStorage;
import android.os.Environment;
import android.os.IBinder;
+import android.os.Handler;
import android.os.UserHandle;
import android.os.storage.StorageEventListener;
import android.os.storage.StorageManager;
@@ -47,6 +49,7 @@ import java.util.HashMap;
public class MtpService extends Service {
private static final String TAG = "MtpService";
private static final boolean LOGD = true;
+ private static boolean SHOW_MTP_TOAST = true;
// We restrict PTP to these subdirectories
private static final String[] PTP_DIRECTORIES = new String[] {
@@ -63,6 +66,8 @@ public class MtpService extends Service {
"DEVPATH=/devices/virtual/misc/mtp_usb";
/// M: @}
+ private Handler mHandler;
+
// Add for update Storage
private boolean mIsSDExist = false;
private static final String SD_EXIST = "SD_EXIST";
@@ -246,6 +251,7 @@ public class MtpService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
+ mHandler = new Handler();
synchronized (mBinder) {
updateDisabledStateLocked();
isUsbConfigured = (intent == null ? false
@@ -267,7 +273,6 @@ public class MtpService extends Service {
} else {
File file = new File(USER_SPACE);
File file_d = new File(USER_SPACE_END);
if (!file.exists() && !file.isDirectory() &&
!file_d.exists() && !file_d.isDirectory()) {
file_d.mkdirs();
@@ -295,6 +300,23 @@ public class MtpService extends Service {
return START_STICKY;
}
+ private class ToastRunnable implements Runnable {
+ String mText;
+
+ public ToastRunnable(String text) {
+ mText = text;
+ }
+
+ @Override
+ public void run() {
+ Toast.makeText(getApplicationContext(), mText, Toast.LENGTH_SHORT).show();
+ }
+ }
+
+ private void showMtpToast() {
+ mHandler.post(new ToastRunnable(getBaseContext().getString(R.string.toast_text)));
+ }
+
private void updateDisabledStateLocked() {
final boolean isCurrentUser = UserHandle.myUserId() == ActivityManager.getCurrentUser();
final KeyguardManager keyguardManager = (KeyguardManager) getSystemService(
@@ -377,6 +399,11 @@ public class MtpService extends Service {
public void sendObjectAdded(int objectHandle) {
synchronized (mBinder) {
if (mServer != null) {
+ if (mServer.showMtpToast() && SHOW_MTP_TOAST) {
+ SHOW_MTP_TOAST = false;
+ showMtpToast();
+ MtkLog.e(TAG, "===== MTP TOAST ===");
+ }
mServer.sendObjectAdded(objectHandle);
}
}
index 31c3ecf..a0b4b77 100644
--- a/frameworks/av/media/mtp/MtpServer.cpp
+++ b/frameworks/av/media/mtp/MtpServer.cpp
@@ -50,6 +50,9 @@
namespace android {
+static int objAddCount;
+static bool showToast = false;
+
static const MtpOperationCode kSupportedOperationCodes[] = {
MTP_OPERATION_GET_DEVICE_INFO,
MTP_OPERATION_OPEN_SESSION,
@@ -392,7 +395,12 @@ void MtpServer::run() {
mFD = -1;
}
+bool MtpServer::showMtpToast() {
+ return showToast;
+}
+
void MtpServer::sendObjectAdded(MtpObjectHandle handle) {
+ objAddCount = handle;</span>
ALOGI("sendObjectAdded %d\n", handle);
sendEvent(MTP_EVENT_OBJECT_ADDED, handle);
}
@@ -441,6 +449,8 @@ void MtpServer::sendEvent(MtpEventCode code, uint32_t param1) {
sxlog_printf(ANDROID_LOG_ERROR, "MtpServer",
"mEvent.write returned %d, errno: %d\n", ret, errno);
+ if (ETIME == errno && objAddCount > 0) showToast = true;
+
if(code == MTP_EVENT_STORE_ADDED || code == MTP_EVENT_STORE_REMOVED)
{
//File under Transfer, get or send, backup the event and send out after file transfer
@@ -451,6 +461,8 @@ void MtpServer::sendEvent(MtpEventCode code, uint32_t param1) {
mBackupStorageId = param1;
}
}
+ } else {
+ showToast = false;
}
//Added Modification for ALPS00276320
}
diff --git a/frameworks/av/media/mtp/MtpServer.h b/frameworks/av/media/mtp/MtpServer.h
index fcf6a81..2a90e93 100644
--- a/frameworks/av/media/mtp/MtpServer.h
+++ b/frameworks/av/media/mtp/MtpServer.h
@@ -115,6 +115,7 @@ public:
void run();
+ bool showMtpToast();
void sendObjectAdded(MtpObjectHandle handle);
void sendObjectRemoved(MtpObjectHandle handle);
//ALPS00289309, update Object
diff --git a/frameworks/base/media/java/android/mtp/MtpServer.java b/frameworks/base/media/java/android/mtp/MtpServer.java
index 678d141..3ef987d 100644
--- a/frameworks/base/media/java/android/mtp/MtpServer.java
+++ b/frameworks/base/media/java/android/mtp/MtpServer.java
@@ -70,6 +70,10 @@ public class MtpServer implements Runnable {
/// M: @}
}
+ public boolean showMtpToast() {
+ return native_show_mtp_toast();
+ }
+
public void sendObjectAdded(int handle) {
native_send_object_added(handle);
}
@@ -139,6 +143,7 @@ public class MtpServer implements Runnable {
private native final void native_setup(MtpDatabase database, boolean usePtp);
private native final void native_run();
private native final void native_cleanup();
+ private native final boolean native_show_mtp_toast();</span>
private native final void native_send_object_added(int handle);
private native final void native_send_object_removed(int handle);
private native final void native_add_storage(MtpStorage storage);
diff --git a/frameworks/base/media/jni/android_mtp_MtpServer.cpp b/frameworks/base/media/jni/android_mtp_MtpServer.cpp
index debfa11..179c2ec 100644
--- a/frameworks/base/media/jni/android_mtp_MtpServer.cpp
+++ b/frameworks/base/media/jni/android_mtp_MtpServer.cpp
@@ -112,6 +112,20 @@ android_mtp_MtpServer_cleanup(JNIEnv *env, jobject thiz)
}
}
+static bool
+android_mtp_MtpServer_show_mtp_toast(JNIEnv *env, jobject thiz, jint handle)
+{
+ Mutex::Autolock autoLock(sMutex);
+
+ MtpServer* server = getMtpServer(env, thiz);
+ if (server)
+ return server->showMtpToast();
+ else
+ ALOGE("server is null in show_mtp_toast");
+
+ return false;
+}
+
static void
android_mtp_MtpServer_send_object_added(JNIEnv *env, jobject thiz, jint handle)
{
@@ -287,6 +301,7 @@ static JNINativeMethod gMethods[] = {
(void *)android_mtp_MtpServer_setup},
{"native_run", "()V", (void *)android_mtp_MtpServer_run},
{"native_cleanup", "()V", (void *)android_mtp_MtpServer_cleanup},
+ {"native_show_mtp_toast", "()Z", (void *)android_mtp_MtpServer_show_mtp_toast},</span>
{"native_send_object_added", "(I)V", (void *)android_mtp_MtpServer_send_object_added},
{"native_send_object_removed", "(I)V", (void *)android_mtp_MtpServer_send_object_removed},
{"native_add_storage", "(Landroid/mtp/MtpStorage;)V",
diff --git a/packages/providers/MediaProvider/res/values-zh-rCN/strings.xml b/packages/providers/MediaProvider/res/values-zh-rCN/strings.xml
index 9bf46c9..48f45a4 100644
--- a/packages/providers/MediaProvider/res/values-zh-rCN/strings.xml
+++ b/packages/providers/MediaProvider/res/values-zh-rCN/strings.xml
@@ -26,4 +26,5 @@
<string name="root_images" msgid="7098113056247445324">"圖片"</string>
<string name="root_videos" msgid="3304457332406057833">"視訊"</string>
<string name="root_audio" msgid="7177565505195715659">"音訊"</string>
+ <string name="toast_text" msgid="7188565505195715659">"MTP錯誤,請重啟裝置。"</string>
</resources>
diff --git a/packages/providers/MediaProvider/res/values/strings.xml b/packages/providers/MediaProvider/res/values/strings.xml
index 8e8c541..8d09925 100644
--- a/packages/providers/MediaProvider/res/values/strings.xml
+++ b/packages/providers/MediaProvider/res/values/strings.xml
@@ -43,5 +43,6 @@
<string name="root_videos">Videos</string>
<!-- Title for documents backend that offers audio. [CHAR LIMIT=24] -->
<string name="root_audio">Audio</string>
+ <string name="toast_text">MTP error, please reboot vinci.</string>
</resources>
diff --git a/packages/providers/MediaProvider/src/com/android/providers/media/MtpService.java b/packages/providers/MediaProvider/src/com/android/providers/media/MtpService.java
index a6edf48..a16b53c 100644
--- a/packages/providers/MediaProvider/src/com/android/providers/media/MtpService.java
+++ b/packages/providers/MediaProvider/src/com/android/providers/media/MtpService.java
@@ -19,6 +19,7 @@ package com.android.providers.media;
import android.app.ActivityManager;
import android.app.KeyguardManager;
import android.app.Service;
+import android.widget.Toast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
@@ -29,6 +30,7 @@ import android.mtp.MtpServer;
import android.mtp.MtpStorage;
import android.os.Environment;
import android.os.IBinder;
+import android.os.Handler;
import android.os.UserHandle;
import android.os.storage.StorageEventListener;
import android.os.storage.StorageManager;
@@ -47,6 +49,7 @@ import java.util.HashMap;
public class MtpService extends Service {
private static final String TAG = "MtpService";
private static final boolean LOGD = true;
+ private static boolean SHOW_MTP_TOAST = true;
// We restrict PTP to these subdirectories
private static final String[] PTP_DIRECTORIES = new String[] {
@@ -63,6 +66,8 @@ public class MtpService extends Service {
"DEVPATH=/devices/virtual/misc/mtp_usb";
/// M: @}
+ private Handler mHandler;
+
// Add for update Storage
private boolean mIsSDExist = false;
private static final String SD_EXIST = "SD_EXIST";
@@ -246,6 +251,7 @@ public class MtpService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
+ mHandler = new Handler();
synchronized (mBinder) {
updateDisabledStateLocked();
isUsbConfigured = (intent == null ? false
@@ -267,7 +273,6 @@ public class MtpService extends Service {
} else {
File file = new File(USER_SPACE);
File file_d = new File(USER_SPACE_END);
if (!file.exists() && !file.isDirectory() &&
!file_d.exists() && !file_d.isDirectory()) {
file_d.mkdirs();
@@ -295,6 +300,23 @@ public class MtpService extends Service {
return START_STICKY;
}
+ private class ToastRunnable implements Runnable {
+ String mText;
+
+ public ToastRunnable(String text) {
+ mText = text;
+ }
+
+ @Override
+ public void run() {
+ Toast.makeText(getApplicationContext(), mText, Toast.LENGTH_SHORT).show();
+ }
+ }
+
+ private void showMtpToast() {
+ mHandler.post(new ToastRunnable(getBaseContext().getString(R.string.toast_text)));
+ }
+
private void updateDisabledStateLocked() {
final boolean isCurrentUser = UserHandle.myUserId() == ActivityManager.getCurrentUser();
final KeyguardManager keyguardManager = (KeyguardManager) getSystemService(
@@ -377,6 +399,11 @@ public class MtpService extends Service {
public void sendObjectAdded(int objectHandle) {
synchronized (mBinder) {
if (mServer != null) {
+ if (mServer.showMtpToast() && SHOW_MTP_TOAST) {
+ SHOW_MTP_TOAST = false;
+ showMtpToast();
+ MtkLog.e(TAG, "===== MTP TOAST ===");
+ }
mServer.sendObjectAdded(objectHandle);
}
}