1 /* 2 * Copyright (C) 2018 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.server.wifi.util; 18 19 import android.os.WorkSource; 20 import android.util.Pair; 21 22 import java.util.ArrayList; 23 import java.util.List; 24 25 /** 26 * Class for general helper methods for WorkSource operations. 27 */ 28 public class WorkSourceUtil { 29 30 /** 31 * Retrieve an array of uids & tags within the worksource. 32 */ getUidsAndTagsForWs(WorkSource ws)33 public static Pair<int[], String[]> getUidsAndTagsForWs(WorkSource ws) { 34 List<Integer> uids = new ArrayList<>(); 35 List<String> tags = new ArrayList<>(); 36 37 for (int i = 0; i < ws.size(); i++) { 38 uids.add(ws.getUid(i)); 39 tags.add(ws.getPackageName(i)); 40 } 41 42 final List<WorkSource.WorkChain> workChains = ws.getWorkChains(); 43 if (workChains != null) { 44 for (int i = 0; i < workChains.size(); ++i) { 45 final WorkSource.WorkChain workChain = workChains.get(i); 46 uids.add(workChain.getAttributionUid()); 47 tags.add(workChain.getAttributionTag()); 48 } 49 } 50 return Pair.create( 51 uids.stream().mapToInt(Integer::intValue).toArray(), 52 tags.toArray(new String[0])); 53 } 54 55 56 } 57