Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit 64ded73

Browse files
authored
Merge pull request #883 from iankchristie/PWSSharing
Send Url initially to PWS
2 parents 384bc28 + e914e24 commit 64ded73

File tree

2 files changed

+43
-46
lines changed

2 files changed

+43
-46
lines changed

android/PhysicalWeb/app/src/main/java/org/physical_web/physicalweb/PhysicalWebBroadcastService.java

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ public class PhysicalWebBroadcastService extends Service {
6262
private NotificationManagerCompat mNotificationManager;
6363
private Handler mHandler = new Handler();
6464
private String mDisplayUrl;
65-
private byte[] mShareUrl;
6665
private boolean mStartedByRestart;
6766

6867
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@@ -110,63 +109,34 @@ public int onStartCommand(Intent intent, int flags, int startId) {
110109
IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
111110
registerReceiver(mReceiver, filter);
112111

113-
Log.d(TAG, mDisplayUrl);
114-
byte[] encodedUrl = AdvertiseDataUtils.encodeUri(mDisplayUrl);
115-
if (hasValidUrlLength(encodedUrl.length) && checkAndHandleAsciiUrl(mDisplayUrl)) {
116-
// Set the url if we can
117-
Log.d(TAG, "valid length");
118-
mShareUrl = encodedUrl;
119-
broadcastUrl();
120-
} else {
121-
Log.d(TAG, "needs shortening");
122-
UrlShortenerClient.ShortenUrlCallback urlSetter =
123-
new UrlShortenerClient.ShortenUrlCallback() {
124-
@Override
125-
public void onUrlShortened(String newUrl) {
126-
Log.d(TAG, "shortening success");
127-
mShareUrl = AdvertiseDataUtils.encodeUri(newUrl);
128-
broadcastUrl();
129-
}
130-
@Override
131-
public void onError(String oldUrl) {
132-
Toast.makeText(getApplicationContext(), getString(R.string.shorten_error),
133-
Toast.LENGTH_LONG).show();
134-
stopSelf();
135-
}
136-
};
137-
UrlShortenerClient shortenerClient = UrlShortenerClient.getInstance(this);
112+
handleUrl();
138113

139-
if (mDisplayUrl.contains("goo.gl")) {
140-
// find the site URL and reshorten it since
141-
// goo.gl will not reshorten other goo.gl links
142-
fetchAndShorten(shortenerClient, urlSetter);
143-
} else {
144-
shortenerClient.shortenUrl(mDisplayUrl, urlSetter, TAG);
145-
}
146-
}
147114
return START_STICKY;
148115
}
149116

150-
private void fetchAndShorten(UrlShortenerClient shortenerClient,
151-
UrlShortenerClient.ShortenUrlCallback urlSetter) {
152-
PwsClient pwsClient = new PwsClient();
153-
pwsClient.resolve(Arrays.asList(mDisplayUrl), new PwsResultCallback() {
117+
private void handleUrl() {
118+
PwsClient pwsClient = new PwsClient();
119+
pwsClient.resolve(Arrays.asList(mDisplayUrl), new PwsResultCallback() {
154120
@Override
155121
public void onPwsResult(PwsResult pwsResult) {
156-
shortenerClient.shortenUrl(pwsResult.getSiteUrl(), urlSetter, TAG);
122+
String fullUrl = pwsResult.getSiteUrl();
123+
byte[] encodedUrl = AdvertiseDataUtils.encodeUri(fullUrl);
124+
if(checkNeedsShortening(encodedUrl, fullUrl)) {
125+
shortenAndBroadcastUrl(fullUrl);
126+
} else {
127+
broadcastUrl(encodedUrl);
128+
}
157129
}
158130

159131
@Override
160132
public void onPwsResultAbsent(String url) {
161-
Toast.makeText(getApplicationContext(), getString(R.string.shorten_error),
162-
Toast.LENGTH_LONG).show();
133+
toastError(R.string.invalid_url_error);
163134
stopSelf();
164135
}
165136

166137
@Override
167138
public void onPwsResultError(Collection<String> urls, int httpResponseCode, Exception e) {
168-
Toast.makeText(getApplicationContext(), getString(R.string.shorten_error),
169-
Toast.LENGTH_LONG).show();
139+
toastError(R.string.invalid_url_error);
170140
stopSelf();
171141
}
172142

@@ -176,6 +146,25 @@ public void onResponseReceived(long durationMillis) {
176146
});
177147
}
178148

149+
private void shortenAndBroadcastUrl(String fullUrl) {
150+
UrlShortenerClient.ShortenUrlCallback urlSetter =
151+
new UrlShortenerClient.ShortenUrlCallback() {
152+
@Override
153+
public void onUrlShortened(String newUrl) {
154+
broadcastUrl(AdvertiseDataUtils.encodeUri(newUrl));
155+
}
156+
@Override
157+
public void onError(String oldUrl) {
158+
toastError(R.string.shorten_error);
159+
stopSelf();
160+
}
161+
};
162+
163+
UrlShortenerClient shortenerClient = UrlShortenerClient.getInstance(this);
164+
165+
shortenerClient.shortenUrl(fullUrl, urlSetter, TAG);
166+
}
167+
179168
private void fetchBroadcastData(Intent intent) {
180169
mStartedByRestart = intent == null;
181170
if (intent == null) {
@@ -189,6 +178,10 @@ private void fetchBroadcastData(Intent intent) {
189178
.commit();
190179
}
191180

181+
private boolean checkNeedsShortening(byte[] encodedUrl, String fullUrl) {
182+
return !hasValidUrlLength(encodedUrl.length) || !checkAndHandleAsciiUrl(fullUrl);
183+
}
184+
192185
private static boolean hasValidUrlLength(int uriLength) {
193186
return 0 < uriLength && uriLength <= MAX_URI_LENGTH;
194187
}
@@ -200,7 +193,7 @@ private boolean checkAndHandleAsciiUrl(String url) {
200193
String urlString = uri.toASCIIString();
201194
isCompliant = url.equals(urlString);
202195
} catch (URISyntaxException e) {
203-
Toast.makeText(this, getString(R.string.no_url_error), Toast.LENGTH_LONG).show();
196+
toastError(R.string.no_url_error);
204197
}
205198
return isCompliant;
206199
}
@@ -249,8 +242,8 @@ public void onStartFailure(int result) {
249242
/////////////////////////////////
250243

251244
// Broadcast via bluetooth the stored URL
252-
private void broadcastUrl() {
253-
final AdvertiseData advertisementData = AdvertiseDataUtils.getAdvertisementData(mShareUrl);
245+
private void broadcastUrl(byte[] url) {
246+
final AdvertiseData advertisementData = AdvertiseDataUtils.getAdvertisementData(url);
254247
final AdvertiseSettings advertiseSettings = AdvertiseDataUtils.getAdvertiseSettings(false);
255248
mBluetoothLeAdvertiser.stopAdvertising(mAdvertiseCallback);
256249
mBluetoothLeAdvertiser.startAdvertising(advertiseSettings,
@@ -272,4 +265,7 @@ public void onReceive(Context context, Intent intent) {
272265
}
273266
};
274267

268+
private void toastError(int messageId) {
269+
Toast.makeText(getApplicationContext(), getString(messageId), Toast.LENGTH_LONG).show();
270+
}
275271
}

android/PhysicalWeb/app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<string name="url_broadcast">URL is broadcasting!</string>
2222
<string name="broadcast_notif">Physical Web is broadcasting a URL</string>
2323
<string name="shorten_error">Could not shorten URL</string>
24+
<string name="invalid_url_error">Invalid Url</string>
2425
<string name="loc_permission">Needs Location permission</string>
2526
<string name="stop">Stop</string>
2627
<string name="pws_endpoint_setting_display">Physical Web Service</string>

0 commit comments

Comments
 (0)