• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package org.chromium.latency.walt;
18 
19 import android.content.Context;
20 import android.support.v4.content.AsyncTaskLoader;
21 
22 import java.io.BufferedOutputStream;
23 import java.io.IOException;
24 import java.io.PrintWriter;
25 import java.net.HttpURLConnection;
26 import java.net.URL;
27 
28 class LogUploader extends AsyncTaskLoader<Integer> {
29 
30     private String urlString;
31     private SimpleLogger logger;
32 
LogUploader(Context context)33     LogUploader(Context context) {
34         super(context);
35         urlString = Utils.getStringPreference(context, R.string.preference_log_url, "");
36         logger = SimpleLogger.getInstance(context);
37 
38     }
39 
LogUploader(Context context, String urlString)40     LogUploader(Context context, String urlString) {
41         super(context);
42         this.urlString = urlString;
43         logger = SimpleLogger.getInstance(context);
44     }
45 
46     @Override
loadInBackground()47     public Integer loadInBackground() {
48         if (urlString.isEmpty()) return -1;
49         try {
50             URL url = new URL(urlString);
51             HttpURLConnection urlConnection =
52                     (HttpURLConnection) url.openConnection();
53             urlConnection.setRequestMethod("POST");
54             urlConnection.setDoOutput(true);
55             urlConnection.setRequestProperty("Content-Type", "text/plain");
56             BufferedOutputStream out =
57                     new BufferedOutputStream(urlConnection.getOutputStream());
58             PrintWriter writer = new PrintWriter(out);
59             writer.write(logger.getLogText());
60             writer.flush();
61             final int responseCode = urlConnection.getResponseCode();
62             if (responseCode / 100 == 2) {
63                 logger.log("Log successfully uploaded");
64             } else {
65                 logger.log("Log upload may have failed. Server return status code " + responseCode);
66             }
67             return responseCode;
68         } catch (IOException e) {
69             logger.log("Failed to upload log");
70             return -1;
71         }
72     }
73 
startUpload()74     void startUpload() {
75         super.forceLoad();
76     }
77 
uploadIfAutoEnabled(Context context)78     static void uploadIfAutoEnabled(Context context) {
79         if (Utils.getBooleanPreference(context, R.string.preference_auto_upload_log, false)) {
80             new LogUploader(context).startUpload();
81         }
82     }
83 }
84