Changeset View
Changeset View
Standalone View
Standalone View
src/org/kde/kdeconnect/Plugins/SharePlugin/ShareActivity.java
Show First 20 Lines • Show All 63 Lines • ▼ Show 20 Line(s) | 60 | switch (item.getItemId()) { | |||
---|---|---|---|---|---|
64 | default: | 64 | default: | ||
65 | break; | 65 | break; | ||
66 | } | 66 | } | ||
67 | return true; | 67 | return true; | ||
68 | } | 68 | } | ||
69 | 69 | | |||
70 | private void updateComputerListAction() { | 70 | private void updateComputerListAction() { | ||
71 | updateComputerList(); | 71 | updateComputerList(); | ||
72 | BackgroundService.RunCommand(ShareActivity.this, new BackgroundService.InstanceCallback() { | 72 | BackgroundService.RunCommand(ShareActivity.this, BackgroundService::onNetworkChange); | ||
73 | @Override | | |||
74 | public void onServiceStart(BackgroundService service) { | | |||
75 | service.onNetworkChange(); | | |||
76 | } | | |||
77 | }); | | |||
78 | 73 | | |||
79 | mSwipeRefreshLayout.setRefreshing(true); | 74 | mSwipeRefreshLayout.setRefreshing(true); | ||
80 | new Thread(new Runnable() { | 75 | new Thread(() -> { | ||
81 | @Override | | |||
82 | public void run() { | | |||
83 | try { | 76 | try { | ||
84 | Thread.sleep(1500); | 77 | Thread.sleep(1500); | ||
85 | } catch (InterruptedException ignored) { | 78 | } catch (InterruptedException ignored) { | ||
86 | } | 79 | } | ||
87 | runOnUiThread(new Runnable() { | 80 | runOnUiThread(() -> mSwipeRefreshLayout.setRefreshing(false)); | ||
88 | @Override | | |||
89 | public void run() { | | |||
90 | mSwipeRefreshLayout.setRefreshing(false); | | |||
91 | } | | |||
92 | }); | | |||
93 | } | | |||
94 | }).start(); | 81 | }).start(); | ||
95 | } | 82 | } | ||
96 | 83 | | |||
97 | private void updateComputerList() { | 84 | private void updateComputerList() { | ||
98 | 85 | | |||
99 | final Intent intent = getIntent(); | 86 | final Intent intent = getIntent(); | ||
100 | 87 | | |||
101 | String action = intent.getAction(); | 88 | String action = intent.getAction(); | ||
102 | if (!Intent.ACTION_SEND.equals(action) && !Intent.ACTION_SEND_MULTIPLE.equals(action)) { | 89 | if (!Intent.ACTION_SEND.equals(action) && !Intent.ACTION_SEND_MULTIPLE.equals(action)) { | ||
103 | finish(); | 90 | finish(); | ||
104 | return; | 91 | return; | ||
105 | } | 92 | } | ||
106 | 93 | | |||
107 | BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() { | 94 | BackgroundService.RunCommand(this, service -> { | ||
108 | @Override | | |||
109 | public void onServiceStart(final BackgroundService service) { | | |||
110 | 95 | | |||
111 | Collection<Device> devices = service.getDevices().values(); | 96 | Collection<Device> devices = service.getDevices().values(); | ||
112 | final ArrayList<Device> devicesList = new ArrayList<>(); | 97 | final ArrayList<Device> devicesList = new ArrayList<>(); | ||
113 | final ArrayList<ListAdapter.Item> items = new ArrayList<>(); | 98 | final ArrayList<ListAdapter.Item> items = new ArrayList<>(); | ||
114 | 99 | | |||
115 | items.add(new SectionItem(getString(R.string.share_to))); | 100 | items.add(new SectionItem(getString(R.string.share_to))); | ||
116 | 101 | | |||
117 | for (Device d : devices) { | 102 | for (Device d : devices) { | ||
118 | if (d.isReachable() && d.isPaired()) { | 103 | if (d.isReachable() && d.isPaired()) { | ||
119 | devicesList.add(d); | 104 | devicesList.add(d); | ||
120 | items.add(new EntryItem(d.getName())); | 105 | items.add(new EntryItem(d.getName())); | ||
121 | } | 106 | } | ||
122 | } | 107 | } | ||
123 | 108 | | |||
124 | runOnUiThread(new Runnable() { | 109 | runOnUiThread(() -> { | ||
125 | @Override | | |||
126 | public void run() { | | |||
127 | ListView list = (ListView) findViewById(R.id.listView1); | 110 | ListView list = (ListView) findViewById(R.id.listView1); | ||
128 | list.setAdapter(new ListAdapter(ShareActivity.this, items)); | 111 | list.setAdapter(new ListAdapter(ShareActivity.this, items)); | ||
129 | list.setOnItemClickListener(new AdapterView.OnItemClickListener() { | 112 | list.setOnItemClickListener((adapterView, view, i, l) -> { | ||
130 | @Override | | |||
131 | public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { | | |||
132 | 113 | | |||
133 | Device device = devicesList.get(i - 1); //NOTE: -1 because of the title! | 114 | Device device = devicesList.get(i - 1); //NOTE: -1 because of the title! | ||
134 | SharePlugin.share(intent, device); | 115 | SharePlugin.share(intent, device); | ||
135 | finish(); | 116 | finish(); | ||
136 | } | | |||
137 | }); | 117 | }); | ||
138 | } | | |||
139 | }); | 118 | }); | ||
140 | 119 | | |||
141 | } | | |||
142 | }); | 120 | }); | ||
143 | } | 121 | } | ||
144 | 122 | | |||
145 | @Override | 123 | @Override | ||
146 | protected void onCreate(Bundle savedInstanceState) { | 124 | protected void onCreate(Bundle savedInstanceState) { | ||
147 | super.onCreate(savedInstanceState); | 125 | super.onCreate(savedInstanceState); | ||
148 | setContentView(R.layout.activity_refresh_list); | 126 | setContentView(R.layout.activity_refresh_list); | ||
149 | 127 | | |||
150 | ActionBar actionBar = getSupportActionBar(); | 128 | ActionBar actionBar = getSupportActionBar(); | ||
151 | mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh_list_layout); | 129 | mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh_list_layout); | ||
152 | mSwipeRefreshLayout.setOnRefreshListener( | 130 | mSwipeRefreshLayout.setOnRefreshListener( | ||
153 | new SwipeRefreshLayout.OnRefreshListener() { | 131 | this::updateComputerListAction | ||
154 | @Override | | |||
155 | public void onRefresh() { | | |||
156 | updateComputerListAction(); | | |||
157 | } | | |||
158 | } | | |||
159 | ); | 132 | ); | ||
160 | if (actionBar != null) { | 133 | if (actionBar != null) { | ||
161 | actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM); | 134 | actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM); | ||
162 | } | 135 | } | ||
163 | } | 136 | } | ||
164 | 137 | | |||
165 | 138 | | |||
166 | @Override | 139 | @Override | ||
167 | protected void onStart() { | 140 | protected void onStart() { | ||
168 | super.onStart(); | 141 | super.onStart(); | ||
169 | 142 | | |||
170 | final Intent intent = getIntent(); | 143 | final Intent intent = getIntent(); | ||
171 | final String deviceId = intent.getStringExtra("deviceId"); | 144 | final String deviceId = intent.getStringExtra("deviceId"); | ||
172 | 145 | | |||
173 | if (deviceId != null) { | 146 | if (deviceId != null) { | ||
174 | 147 | | |||
175 | BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() { | 148 | BackgroundService.RunCommand(this, service -> { | ||
176 | | ||||
177 | @Override | | |||
178 | public void onServiceStart(BackgroundService service) { | | |||
179 | Log.d("DirectShare", "sharing to " + service.getDevice(deviceId).getName()); | 149 | Log.d("DirectShare", "sharing to " + service.getDevice(deviceId).getName()); | ||
180 | Device device = service.getDevice(deviceId); | 150 | Device device = service.getDevice(deviceId); | ||
181 | if (device.isReachable() && device.isPaired()) { | 151 | if (device.isReachable() && device.isPaired()) { | ||
182 | SharePlugin.share(intent, device); | 152 | SharePlugin.share(intent, device); | ||
183 | } | 153 | } | ||
184 | finish(); | 154 | finish(); | ||
185 | } | | |||
186 | }); | 155 | }); | ||
187 | } else { | 156 | } else { | ||
188 | 157 | | |||
189 | BackgroundService.addGuiInUseCounter(this); | 158 | BackgroundService.addGuiInUseCounter(this); | ||
190 | BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() { | 159 | BackgroundService.RunCommand(this, service -> { | ||
191 | @Override | | |||
192 | public void onServiceStart(BackgroundService service) { | | |||
193 | service.onNetworkChange(); | 160 | service.onNetworkChange(); | ||
194 | service.addDeviceListChangedCallback("ShareActivity", new BackgroundService.DeviceListChangedCallback() { | 161 | service.addDeviceListChangedCallback("ShareActivity", this::updateComputerList); | ||
195 | @Override | | |||
196 | public void onDeviceListChanged() { | | |||
197 | updateComputerList(); | | |||
198 | } | | |||
199 | }); | | |||
200 | } | | |||
201 | }); | 162 | }); | ||
202 | updateComputerList(); | 163 | updateComputerList(); | ||
203 | } | 164 | } | ||
204 | } | 165 | } | ||
205 | 166 | | |||
206 | 167 | | |||
207 | @Override | 168 | @Override | ||
208 | protected void onStop() { | 169 | protected void onStop() { | ||
209 | BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() { | 170 | BackgroundService.RunCommand(this, service -> service.removeDeviceListChangedCallback("ShareActivity")); | ||
210 | @Override | | |||
211 | public void onServiceStart(BackgroundService service) { | | |||
212 | service.removeDeviceListChangedCallback("ShareActivity"); | | |||
213 | } | | |||
214 | }); | | |||
215 | BackgroundService.removeGuiInUseCounter(this); | 171 | BackgroundService.removeGuiInUseCounter(this); | ||
216 | super.onStop(); | 172 | super.onStop(); | ||
217 | } | 173 | } | ||
218 | 174 | | |||
219 | } | 175 | } |