• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 com.android.tv.common.util;
18 
19 import android.net.TrafficStats;
20 import android.support.annotation.NonNull;
21 import java.util.concurrent.Executor;
22 
23 /** Constants for tagging network traffic in the Live channels app. */
24 public final class NetworkTrafficTags {
25 
26     public static final int DEFAULT_LIVE_CHANNELS = 1;
27     public static final int LOGO_FETCHER = 2;
28     public static final int HDHOMERUN = 3;
29     public static final int EPG_FETCH = 4;
30 
31     /**
32      * An executor which simply wraps a provided delegate executor, but calls {@link
33      * TrafficStats#setThreadStatsTag(int)} before executing any task.
34      */
35     public static class TrafficStatsTaggingExecutor implements Executor {
36         private final Executor delegateExecutor;
37         private final int tag;
38 
TrafficStatsTaggingExecutor(Executor delegateExecutor, int tag)39         public TrafficStatsTaggingExecutor(Executor delegateExecutor, int tag) {
40             this.delegateExecutor = delegateExecutor;
41             this.tag = tag;
42         }
43 
44         @Override
execute(final @NonNull Runnable command)45         public void execute(final @NonNull Runnable command) {
46       // TODO(b/62038127): robolectric does not support lamdas in unbundled apps
47       delegateExecutor.execute(
48           () -> {
49             TrafficStats.setThreadStatsTag(tag);
50             try {
51               command.run();
52             } finally {
53               TrafficStats.clearThreadStatsTag();
54             }
55           });
56         }
57     }
58 
NetworkTrafficTags()59     private NetworkTrafficTags() {}
60 }
61