2012-08-05 06:26:35 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* 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
|
2012-08-05 06:26:35 +00:00
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-08-05 06:26:35 +00:00
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
2013-01-21 12:52:57 +00:00
|
|
|
* 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.
|
2012-08-05 06:26:35 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.research;
|
|
|
|
|
|
|
|
import android.Manifest;
|
|
|
|
import android.app.AlarmManager;
|
|
|
|
import android.app.IntentService;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.IntentFilter;
|
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
import android.net.ConnectivityManager;
|
|
|
|
import android.net.NetworkInfo;
|
|
|
|
import android.os.BatteryManager;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import com.android.inputmethod.latin.R;
|
2012-12-19 19:27:51 +00:00
|
|
|
import com.android.inputmethod.latin.define.ProductionFlag;
|
2012-08-05 06:26:35 +00:00
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileFilter;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
|
|
|
|
|
|
|
public final class UploaderService extends IntentService {
|
|
|
|
private static final String TAG = UploaderService.class.getSimpleName();
|
2012-09-11 20:44:56 +00:00
|
|
|
private static final boolean DEBUG = false && ProductionFlag.IS_EXPERIMENTAL_DEBUG;
|
2012-12-19 19:27:51 +00:00
|
|
|
// Set IS_INHIBITING_AUTO_UPLOAD to true for local testing
|
2012-09-11 20:44:56 +00:00
|
|
|
private static final boolean IS_INHIBITING_AUTO_UPLOAD = false
|
|
|
|
&& ProductionFlag.IS_EXPERIMENTAL_DEBUG; // Force false in production
|
2012-08-05 06:26:35 +00:00
|
|
|
public static final long RUN_INTERVAL = AlarmManager.INTERVAL_HOUR;
|
2013-01-22 01:12:44 +00:00
|
|
|
public static final String EXTRA_UPLOAD_UNCONDITIONALLY = UploaderService.class.getName()
|
2012-08-05 06:26:35 +00:00
|
|
|
+ ".extra.UPLOAD_UNCONDITIONALLY";
|
|
|
|
private static final int BUF_SIZE = 1024 * 8;
|
|
|
|
protected static final int TIMEOUT_IN_MS = 1000 * 4;
|
|
|
|
|
|
|
|
private File mFilesDir;
|
|
|
|
private URL mUrl;
|
|
|
|
|
|
|
|
public UploaderService() {
|
|
|
|
super("Research Uploader Service");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
|
|
|
super.onCreate();
|
|
|
|
|
|
|
|
mFilesDir = null;
|
|
|
|
mUrl = null;
|
|
|
|
|
2013-02-27 20:06:41 +00:00
|
|
|
if (!hasUploadingPermission()) {
|
2012-08-05 06:26:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
final String urlString = getString(R.string.research_logger_upload_url);
|
|
|
|
if (urlString == null || urlString.equals("")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mFilesDir = getFilesDir();
|
|
|
|
mUrl = new URL(urlString);
|
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-27 20:13:42 +00:00
|
|
|
public boolean isPossibleToUpload() {
|
|
|
|
return hasUploadingPermission() && mUrl != null && !IS_INHIBITING_AUTO_UPLOAD;
|
|
|
|
}
|
|
|
|
|
2013-02-27 20:06:41 +00:00
|
|
|
private boolean hasUploadingPermission() {
|
|
|
|
final PackageManager packageManager = getPackageManager();
|
|
|
|
return packageManager.checkPermission(Manifest.permission.INTERNET,
|
|
|
|
getPackageName()) == PackageManager.PERMISSION_GRANTED;
|
|
|
|
}
|
|
|
|
|
2012-08-05 06:26:35 +00:00
|
|
|
@Override
|
|
|
|
protected void onHandleIntent(Intent intent) {
|
2013-02-27 20:13:42 +00:00
|
|
|
if (!isPossibleToUpload()) return;
|
2013-02-27 20:32:39 +00:00
|
|
|
if (isUploadingUnconditionally(intent.getExtras()) || isConvenientToUpload()) {
|
|
|
|
doUpload();
|
|
|
|
}
|
2013-02-27 20:17:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isUploadingUnconditionally(final Bundle bundle) {
|
|
|
|
if (bundle == null) return false;
|
|
|
|
if (bundle.containsKey(EXTRA_UPLOAD_UNCONDITIONALLY)) {
|
|
|
|
return bundle.getBoolean(EXTRA_UPLOAD_UNCONDITIONALLY);
|
2012-08-05 06:26:35 +00:00
|
|
|
}
|
2013-02-27 20:17:49 +00:00
|
|
|
return false;
|
2012-08-05 06:26:35 +00:00
|
|
|
}
|
|
|
|
|
2013-02-27 20:27:50 +00:00
|
|
|
private boolean isConvenientToUpload() {
|
|
|
|
return isExternallyPowered() && hasWifiConnection();
|
|
|
|
}
|
|
|
|
|
2012-08-05 06:26:35 +00:00
|
|
|
private boolean isExternallyPowered() {
|
|
|
|
final Intent intent = registerReceiver(null, new IntentFilter(
|
|
|
|
Intent.ACTION_BATTERY_CHANGED));
|
|
|
|
final int pluggedState = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
|
|
|
|
return pluggedState == BatteryManager.BATTERY_PLUGGED_AC
|
|
|
|
|| pluggedState == BatteryManager.BATTERY_PLUGGED_USB;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean hasWifiConnection() {
|
|
|
|
final ConnectivityManager manager =
|
|
|
|
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
|
final NetworkInfo wifiInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
|
|
|
return wifiInfo.isConnected();
|
|
|
|
}
|
|
|
|
|
2013-02-27 20:32:39 +00:00
|
|
|
private void doUpload() {
|
2012-08-05 06:26:35 +00:00
|
|
|
if (mFilesDir == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final File[] files = mFilesDir.listFiles(new FileFilter() {
|
|
|
|
@Override
|
|
|
|
public boolean accept(File pathname) {
|
2013-01-21 23:13:05 +00:00
|
|
|
return pathname.getName().startsWith(ResearchLogger.LOG_FILENAME_PREFIX)
|
2012-08-05 06:26:35 +00:00
|
|
|
&& !pathname.canWrite();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
boolean success = true;
|
|
|
|
if (files.length == 0) {
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
for (final File file : files) {
|
|
|
|
if (!uploadFile(file)) {
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean uploadFile(File file) {
|
2012-09-11 20:44:56 +00:00
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, "attempting upload of " + file.getAbsolutePath());
|
|
|
|
}
|
2012-08-05 06:26:35 +00:00
|
|
|
boolean success = false;
|
|
|
|
final int contentLength = (int) file.length();
|
|
|
|
HttpURLConnection connection = null;
|
|
|
|
InputStream fileInputStream = null;
|
|
|
|
try {
|
|
|
|
fileInputStream = new FileInputStream(file);
|
|
|
|
connection = (HttpURLConnection) mUrl.openConnection();
|
|
|
|
connection.setRequestMethod("PUT");
|
|
|
|
connection.setDoOutput(true);
|
|
|
|
connection.setFixedLengthStreamingMode(contentLength);
|
|
|
|
final OutputStream os = connection.getOutputStream();
|
|
|
|
final byte[] buf = new byte[BUF_SIZE];
|
|
|
|
int numBytesRead;
|
|
|
|
while ((numBytesRead = fileInputStream.read(buf)) != -1) {
|
|
|
|
os.write(buf, 0, numBytesRead);
|
2012-09-11 20:44:56 +00:00
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, new String(buf));
|
|
|
|
}
|
2012-08-05 06:26:35 +00:00
|
|
|
}
|
|
|
|
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
|
|
|
|
Log.d(TAG, "upload failed: " + connection.getResponseCode());
|
|
|
|
InputStream netInputStream = connection.getInputStream();
|
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(netInputStream));
|
|
|
|
String line;
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
Log.d(TAG, "| " + reader.readLine());
|
|
|
|
}
|
|
|
|
reader.close();
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
file.delete();
|
|
|
|
success = true;
|
2012-09-11 20:44:56 +00:00
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, "upload successful");
|
|
|
|
}
|
2012-08-05 06:26:35 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} finally {
|
|
|
|
if (fileInputStream != null) {
|
|
|
|
try {
|
|
|
|
fileInputStream.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (connection != null) {
|
|
|
|
connection.disconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
}
|