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
|
/*
* (c) Copyright 2016 Mopria Alliance, Inc.
* (c) Copyright 2016 Samsung Electronics..
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.discovery.recommendation;
import android.content.Context;
import android.net.nsd.NsdManager;
import android.os.Bundle;
import android.support.annotation.IntRange;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.android.discovery.recommendation.PrintServicePlugin;
import com.android.discovery.recommendation.ServiceRecommendationPlugin;
import com.android.discovery.recommendation.ServiceResolveQueue;
import com.samsung.discovery.recommendation.SamsungRecommendationPlugin;
import java.util.Locale;
public class DeviceListFragment extends ListFragment implements PrintServicePlugin.PrinterDiscoveryCallback {
private ArrayAdapter<ServiceRecommendationPlugin> mArrayAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ServiceResolveQueue.createInstance((NsdManager)getActivity().getSystemService(Context.NSD_SERVICE));
mArrayAdapter = new ArrayAdapter<ServiceRecommendationPlugin>(getActivity(), 0) {
class ViewHolder {
final TextView mText1;
final TextView mText2;
public ViewHolder(TextView text1, TextView text2) {
mText1 = text1;
mText2 = text2;
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
ViewHolder holder;
if (convertView == null) {
view = getActivity().getLayoutInflater().inflate(android.R.layout.simple_list_item_2, parent, false);
view.setTag(new ViewHolder((TextView)view.findViewById(android.R.id.text1),
(TextView)view.findViewById(android.R.id.text2)));
} else {
view = convertView;
}
holder = (ViewHolder)view.getTag();
holder.mText1.setText(getItem(position).getName());
holder.mText2.setText(String.format(Locale.getDefault(), "%d",getItem(position).getCount()));
return view;
}
};
mArrayAdapter.add(new SamsungRecommendationPlugin(getActivity()));
}
@Override
public void onDestroy() {
super.onDestroy();
ServiceResolveQueue.destroyInstance();
}
@Override
public void onResume() {
super.onResume();
for(int i = 0; i < mArrayAdapter.getCount(); i++) {
try {
mArrayAdapter.getItem(i).start(this);
} catch(Exception ignored) {}
}
}
@Override
public void onPause() {
super.onPause();
for(int i = 0; i < mArrayAdapter.getCount(); i++) {
try {
mArrayAdapter.getItem(i).stop();
} catch(Exception ignored) {}
}
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setListAdapter(mArrayAdapter);
}
@Override
public void onChanged(@IntRange(from = 0) int numDiscoveredPrinters) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
mArrayAdapter.notifyDataSetChanged();
}
});
}
}
|