summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/utils/TileLoadingMeasurementUtils.java
blob: c8af9aee214346d57374b130af9343b5bbd3e9cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package com.mapbox.mapboxsdk.testapp.utils;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StringDef;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.mapbox.mapboxsdk.MapStrictMode;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.module.http.HttpRequestUtil;

import java.io.IOException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import timber.log.Timber;

import static com.mapbox.mapboxsdk.constants.MapboxConstants.DEFAULT_MEASURE_TILE_DOWNLOAD_ON;
import static com.mapbox.mapboxsdk.constants.MapboxConstants.KEY_META_DATA_MEASURE_TILE_DOWNLOAD_ON;

public class TileLoadingMeasurementUtils {

  private static final String ATTRIBUTE_REQUEST_URL = "requestUrl";


  public static void setUpTileLoadingMeasurement() {
    if (isTileLoadingMeasurementOn()) {
      OkHttpClient okHttpClient = new OkHttpClient.Builder()
              .addNetworkInterceptor(new TileLoadingInterceptor())
              .build();
      HttpRequestUtil.setOkHttpClient(okHttpClient);
    }
  }

  private static boolean isTileLoadingMeasurementOn() {
    return isBooleanMetaDataValueOn(KEY_META_DATA_MEASURE_TILE_DOWNLOAD_ON,
            DEFAULT_MEASURE_TILE_DOWNLOAD_ON);
  }

  private static boolean isBooleanMetaDataValueOn(@NonNull String propKey, boolean defaultValue) {

    try {
      // Try getting a custom value from the app Manifest
      Context context = Mapbox.getApplicationContext();
      ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(),
              PackageManager.GET_META_DATA);
      if (appInfo.metaData != null) {
        return appInfo.metaData.getBoolean(propKey, defaultValue);
      }
    } catch (PackageManager.NameNotFoundException exception) {
      Timber.e("Failed to read the package metadata: " + exception);
      MapStrictMode.strictModeViolation(exception);
    } catch (Exception exception) {
      Timber.e("Failed to read key: " + propKey + " " + exception);
      MapStrictMode.strictModeViolation(exception);
    }
    return defaultValue;
  }


  /**
   * This Interceptor allows to measure time spent getting a response object over network.
   * The following data will be collected:
   *  responseCode, elapsedMS
   *  requestUrl (request string till the question mark),
   *  and device metadata.
   */
  static class TileLoadingInterceptor implements Interceptor {

    private static String metadata = null;

    @StringDef( {CONNECTION_NONE, CONNECTION_CELLULAR, CONNECTION_WIFI})
    @Retention(RetentionPolicy.SOURCE)
    @interface ConnectionState {
    }
    private static final String CONNECTION_NONE = "none";
    private static final String CONNECTION_CELLULAR = "cellular";
    private static final String CONNECTION_WIFI = "wifi";

    @Override
    public Response intercept(Chain chain) throws IOException {
      Request request = chain.request();
      long elapsed = System.nanoTime();

      Response response = chain.proceed(request);
      elapsed = System.nanoTime() - elapsed;

      triggerPerformanceEvent(response, elapsed / 1000000);

      return response;
    }

    private void triggerPerformanceEvent(Response response, long elapsedMs) {
      List<Attribute<String>> attributes = new ArrayList<>();
      String request = getUrl(response.request());
      attributes.add(new Attribute<>("requestUrl", request));
      attributes.add(new Attribute<>("responseCode", String.valueOf(response.code())));
      attributes.add(
              new Attribute<>("connectionState", getConnectionState()));

      List<Attribute<Long>> counters = new ArrayList();
      counters.add(new Attribute<>("elapsedMS", elapsedMs));

      Bundle bundle = new Bundle();
      Gson gson = new Gson();
      bundle.putString("attributes", gson.toJson(attributes));
      bundle.putString("counters", gson.toJson(counters));
      bundle.putString("metadata", getMetadata());

      Mapbox.getTelemetry().onPerformanceEvent(bundle);
    }

    private static String getUrl(Request request) {
      String url = request.url().toString();
      return url.substring(0, url.indexOf('?'));
    }

    private static String getMetadata() {
      if (metadata == null) {
        JsonObject metaData = new JsonObject();
        metaData.addProperty("os", "android");
        metaData.addProperty("manufacturer", Build.MANUFACTURER);
        metaData.addProperty("brand", Build.BRAND);
        metaData.addProperty("device", Build.MODEL);
        metaData.addProperty("version", Build.VERSION.RELEASE);
        metaData.addProperty("abi", Build.CPU_ABI);
        metaData.addProperty("country", Locale.getDefault().getISO3Country());
        metaData.addProperty("ram", getRam());
        metaData.addProperty("screenSize", getWindowSize());

        metadata = metaData.toString();
      }
      return metadata;
    }

    private static String getRam() {
      ActivityManager actManager =
              (ActivityManager) Mapbox.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
      ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();
      actManager.getMemoryInfo(memInfo);
      return String.valueOf(memInfo.totalMem);
    }

    private static String getWindowSize() {
      WindowManager windowManager =
              (WindowManager) Mapbox.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
      Display display = windowManager.getDefaultDisplay();
      DisplayMetrics metrics = new DisplayMetrics();
      display.getMetrics(metrics);
      int width = metrics.widthPixels;
      int height = metrics.heightPixels;
      return "{" + width + "," + height + "}";
    }

    @ConnectionState
    private static String getConnectionState() {
      Context appContext = Mapbox.getApplicationContext();
      ConnectivityManager connectivityManager =
              (ConnectivityManager) appContext.getSystemService(Context.CONNECTIVITY_SERVICE);

      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (connectivityManager != null) {
          NetworkCapabilities capabilities =
                  connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork());
          if (capabilities != null) {
            if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
              return CONNECTION_WIFI;
            } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
              return CONNECTION_CELLULAR;
            }
          }
        }
      } else {
        if (connectivityManager != null) {
          NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
          if (activeNetwork != null) {
            if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
              return CONNECTION_WIFI;
            } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
              return CONNECTION_CELLULAR;
            }
          }
        }
      }
      return CONNECTION_NONE;
    }
  }

  private static class Attribute<T> {
    private String name;
    private T value;

    Attribute(String name, T value) {
      this.name = name;
      this.value = value;
    }
  }
}