Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.physicalweb; | 5 package org.chromium.chrome.browser.physicalweb; |
| 6 | 6 |
| 7 import android.app.ListActivity; | 7 import android.app.ListActivity; |
| 8 import android.content.Intent; | 8 import android.content.Intent; |
| 9 import android.graphics.Bitmap; | |
| 9 import android.net.Uri; | 10 import android.net.Uri; |
| 11 import android.os.AsyncTask; | |
| 10 import android.os.Bundle; | 12 import android.os.Bundle; |
| 11 import android.view.View; | 13 import android.view.View; |
| 12 import android.widget.ArrayAdapter; | |
| 13 import android.widget.ListView; | 14 import android.widget.ListView; |
| 14 | 15 |
| 16 import org.chromium.base.Log; | |
| 15 import org.chromium.chrome.R; | 17 import org.chromium.chrome.R; |
| 16 | 18 |
| 19 import java.net.MalformedURLException; | |
| 17 import java.util.Collection; | 20 import java.util.Collection; |
| 18 import java.util.HashSet; | 21 import java.util.HashSet; |
| 19 | 22 |
| 20 /** | 23 /** |
| 21 * This activity displays a list of nearby URLs as stored in the {@link UrlManag er}. | 24 * This activity displays a list of nearby URLs as stored in the {@link UrlManag er}. |
| 22 * This activity does not and should not rely directly or indirectly on the nati ve library. | 25 * This activity does not and should not rely directly or indirectly on the nati ve library. |
| 23 */ | 26 */ |
| 24 public class ListUrlsActivity extends ListActivity { | 27 public class ListUrlsActivity extends ListActivity { |
| 25 private static final String TAG = "PhysicalWeb"; | 28 private static final String TAG = "PhysicalWeb"; |
| 26 private ArrayAdapter<PwsResult> mAdapter; | 29 private NearbyUrlsAdapter mAdapter; |
| 27 private PwsClient mPwsClient; | 30 private PwsClient mPwsClient; |
| 28 | 31 |
| 29 @Override | 32 @Override |
| 30 protected void onCreate(Bundle savedInstanceState) { | 33 protected void onCreate(Bundle savedInstanceState) { |
| 31 super.onCreate(savedInstanceState); | 34 super.onCreate(savedInstanceState); |
| 32 setContentView(R.layout.physical_web_list_urls_activity); | 35 setContentView(R.layout.physical_web_list_urls_activity); |
| 33 | 36 |
| 34 mAdapter = new NearbyUrlsAdapter(this); | 37 mAdapter = new NearbyUrlsAdapter(this); |
| 35 setListAdapter(mAdapter); | 38 setListAdapter(mAdapter); |
| 36 | 39 |
| 37 mPwsClient = new PwsClient(); | 40 mPwsClient = new PwsClient(); |
| 38 } | 41 } |
| 39 | 42 |
| 40 @Override | 43 @Override |
| 41 protected void onResume() { | 44 protected void onResume() { |
| 42 super.onResume(); | 45 super.onResume(); |
| 43 mAdapter.clear(); | 46 mAdapter.clear(); |
| 44 Collection<String> urls = UrlManager.getInstance(this).getUrls(); | 47 Collection<String> urls = UrlManager.getInstance(this).getUrls(); |
| 45 mPwsClient.resolve(urls, new PwsClient.ResolveScanCallback() { | 48 mPwsClient.resolve(urls, new PwsClient.ResolveScanCallback() { |
| 46 @Override | 49 @Override |
| 47 public void onPwsResults(Collection<PwsResult> pwsResults) { | 50 public void onPwsResults(Collection<PwsResult> pwsResults) { |
| 48 // filter out duplicate site URLs | 51 // filter out duplicate site URLs |
| 49 Collection<String> siteUrls = new HashSet<>(); | 52 Collection<String> siteUrls = new HashSet<>(); |
| 50 for (PwsResult pwsResult : pwsResults) { | 53 for (PwsResult pwsResult : pwsResults) { |
| 51 String siteUrl = pwsResult.siteUrl; | 54 String siteUrl = pwsResult.siteUrl; |
| 55 String iconUrl = pwsResult.iconUrl; | |
| 56 | |
| 52 if (siteUrl != null && !siteUrls.contains(siteUrl)) { | 57 if (siteUrl != null && !siteUrls.contains(siteUrl)) { |
| 53 siteUrls.add(siteUrl); | 58 siteUrls.add(siteUrl); |
| 54 mAdapter.add(pwsResult); | 59 mAdapter.add(pwsResult); |
| 60 | |
| 61 if (iconUrl != null) { | |
| 62 fetchIcon(iconUrl); | |
| 63 } | |
| 55 } | 64 } |
| 56 } | 65 } |
| 57 } | 66 } |
| 58 }); | 67 }); |
| 59 } | 68 } |
| 60 | 69 |
| 61 /** | 70 /** |
| 62 * Handle a click event. | 71 * Handle a click event. |
| 63 * @param l The ListView. | 72 * @param l The ListView. |
| 64 * @param v The View that was clicked inside the ListView. | 73 * @param v The View that was clicked inside the ListView. |
| 65 * @param position The position of the clicked element in the list. | 74 * @param position The position of the clicked element in the list. |
| 66 * @param id The row id of the clicked element in the list. | 75 * @param id The row id of the clicked element in the list. |
| 67 */ | 76 */ |
| 68 @Override | 77 @Override |
| 69 public void onListItemClick(ListView l, View v, int position, long id) { | 78 public void onListItemClick(ListView l, View v, int position, long id) { |
| 70 PwsResult pwsResult = mAdapter.getItem(position); | 79 PwsResult pwsResult = mAdapter.getItem(position); |
| 71 Intent intent = createNavigateToUrlIntent(pwsResult); | 80 Intent intent = createNavigateToUrlIntent(pwsResult); |
| 72 startActivity(intent); | 81 startActivity(intent); |
| 73 } | 82 } |
| 74 | 83 |
| 84 private void fetchIcon(final String iconUrl) { | |
| 85 BitmapHttpRequest iconRequest = null; | |
| 86 try { | |
| 87 iconRequest = new BitmapHttpRequest(iconUrl, new BitmapHttpRequest.R equestCallback() { | |
| 88 @Override | |
| 89 public void onResponse(Bitmap iconBitmap) { | |
| 90 mAdapter.setIcon(iconUrl, iconBitmap); | |
| 91 } | |
| 92 | |
| 93 @Override | |
| 94 public void onError(int httpResponseCode, Exception e) { | |
| 95 Log.e(TAG, "Error fetching icon, returned code " + httpRespo nseCode); | |
| 96 } | |
| 97 }); | |
| 98 } catch (MalformedURLException e) { | |
| 99 Log.e(TAG, "Error creating icon request, malformed icon URL"); | |
| 100 } | |
| 101 | |
| 102 if (iconRequest != null) { | |
| 103 // The callback will be called on the main thread. | |
| 104 AsyncTask.THREAD_POOL_EXECUTOR.execute(iconRequest); | |
|
dvh
2015/10/28 23:46:40
I would rather move the threading logic to PwsClie
cco3
2015/10/29 00:38:13
Yea, this whole method could be moved to PwsClient
| |
| 105 } | |
| 106 } | |
| 107 | |
| 75 private static Intent createNavigateToUrlIntent(PwsResult pwsResult) { | 108 private static Intent createNavigateToUrlIntent(PwsResult pwsResult) { |
| 76 String url = pwsResult.siteUrl; | 109 String url = pwsResult.siteUrl; |
| 77 if (url == null) { | 110 if (url == null) { |
| 78 url = pwsResult.requestUrl; | 111 url = pwsResult.requestUrl; |
| 79 } | 112 } |
| 80 | 113 |
| 81 Intent intent = new Intent(Intent.ACTION_VIEW); | 114 Intent intent = new Intent(Intent.ACTION_VIEW); |
| 82 intent.addCategory(Intent.CATEGORY_BROWSABLE); | 115 intent.addCategory(Intent.CATEGORY_BROWSABLE); |
| 83 intent.setData(Uri.parse(url)); | 116 intent.setData(Uri.parse(url)); |
| 84 return intent; | 117 return intent; |
| 85 } | 118 } |
| 86 } | 119 } |
| OLD | NEW |