• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 package com.android.settings.datausage;
15 
16 import android.content.Context;
17 import android.widget.AdapterView;
18 
19 import com.android.settings.Utils;
20 import com.android.settingslib.net.NetworkCycleData;
21 import com.android.settingslib.widget.SettingsSpinnerAdapter;
22 
23 import java.util.List;
24 
25 public class CycleAdapter extends SettingsSpinnerAdapter<CycleAdapter.CycleItem> {
26 
27     private final SpinnerInterface mSpinner;
28     private final AdapterView.OnItemSelectedListener mListener;
29 
CycleAdapter(Context context, SpinnerInterface spinner, AdapterView.OnItemSelectedListener listener)30     public CycleAdapter(Context context, SpinnerInterface spinner,
31             AdapterView.OnItemSelectedListener listener) {
32         super(context);
33         mSpinner = spinner;
34         mListener = listener;
35         mSpinner.setAdapter(this);
36     }
37 
38     /**
39      * Find position of {@link CycleItem} in this adapter which is nearest
40      * the given {@link CycleItem}.
41      */
findNearestPosition(CycleItem target)42     public int findNearestPosition(CycleItem target) {
43         if (target != null) {
44             final int count = getCount();
45             for (int i = count - 1; i >= 0; i--) {
46                 final CycleItem item = getItem(i);
47                 if (item.compareTo(target) >= 0) {
48                     return i;
49                 }
50             }
51         }
52         return 0;
53     }
54 
setInitialCycleList(List<Long> cycles, long selectedCycle)55     void setInitialCycleList(List<Long> cycles, long selectedCycle) {
56         clear();
57         for (int i = 0; i < cycles.size() - 1; i++) {
58             add(new CycleAdapter.CycleItem(getContext(), cycles.get(i + 1), cycles.get(i)));
59             if (cycles.get(i) == selectedCycle) {
60                 mSpinner.setSelection(i);
61             }
62         }
63     }
64 
65     /**
66      * Rebuild list based on network data. Always selects the newest item,
67      * updating the inspection range on chartData.
68      */
updateCycleList(List<? extends NetworkCycleData> cycleData)69     public void updateCycleList(List<? extends NetworkCycleData> cycleData) {
70         mSpinner.setOnItemSelectedListener(mListener);
71         // stash away currently selected cycle to try restoring below
72         final CycleAdapter.CycleItem previousItem = (CycleAdapter.CycleItem)
73                 mSpinner.getSelectedItem();
74         clear();
75 
76         final Context context = getContext();
77         for (NetworkCycleData data : cycleData) {
78             add(new CycleAdapter.CycleItem(context, data.getStartTime(), data.getEndTime()));
79         }
80 
81         // force pick the current cycle (first item)
82         if (getCount() > 0) {
83             final int position = findNearestPosition(previousItem);
84             mSpinner.setSelection(position);
85         }
86     }
87 
88     /**
89      * List item that reflects a specific data usage cycle.
90      */
91     public static class CycleItem implements Comparable<CycleItem> {
92         public CharSequence label;
93         public long start;
94         public long end;
95 
CycleItem(Context context, long start, long end)96         public CycleItem(Context context, long start, long end) {
97             this.label = Utils.formatDateRange(context, start, end);
98             this.start = start;
99             this.end = end;
100         }
101 
102         @Override
toString()103         public String toString() {
104             return label.toString();
105         }
106 
107         @Override
equals(Object o)108         public boolean equals(Object o) {
109             if (o instanceof CycleItem) {
110                 final CycleItem another = (CycleItem) o;
111                 return start == another.start && end == another.end;
112             }
113             return false;
114         }
115 
116         @Override
compareTo(CycleItem another)117         public int compareTo(CycleItem another) {
118             return Long.compare(start, another.start);
119         }
120     }
121 
122     public interface SpinnerInterface {
setAdapter(CycleAdapter cycleAdapter)123         void setAdapter(CycleAdapter cycleAdapter);
124 
setOnItemSelectedListener(AdapterView.OnItemSelectedListener listener)125         void setOnItemSelectedListener(AdapterView.OnItemSelectedListener listener);
126 
getSelectedItem()127         Object getSelectedItem();
128 
setSelection(int position)129         void setSelection(int position);
130     }
131 }
132