Skip to content

Commit 7f0b979

Browse files
committed
Merge pull request googleglass#1 from googleglass/notify
Added timeline echo for photos and timeline notifications
2 parents 82b491c + d3caf0e commit 7f0b979

File tree

8 files changed

+187
-119
lines changed

8 files changed

+187
-119
lines changed

attachment-proxy.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
$_GET['timeline_item_id'], $_GET['attachment_id']);
4848

4949
// set the content type header
50-
header('Content-type: '. $attachment['contentType']);
50+
header('Content-type: '. $attachment->getContentType());
5151

5252
// echo the bytes
53-
echo downloadAttachment($_GET['timeline_item_id'], $attachment);
53+
echo download_attachment($_GET['timeline_item_id'], $attachment);

config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@
2424

2525
$base_url = "http://localhost/mirror-quickstart-php";
2626

27-
$sqlite_database = "/tmp/database.sqlite";
27+
$sqlite_database = "/tmp/database.sqlite";

index.php

Lines changed: 54 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
// But first, handle POST data from the form (if there is any)
3939
switch ($_POST['operation']) {
40-
case "insertItem":
40+
case 'insertItem':
4141
$new_timeline_item = new Google_TimelineItem();
4242
$new_timeline_item->setText($_POST['message']);
4343

@@ -46,53 +46,53 @@
4646
$new_timeline_item->setNotification($notification);
4747

4848
if (isset($_POST['imageUrl']) && isset($_POST['contentType'])) {
49-
insertTimelineItem($mirror_service, $new_timeline_item,
49+
insert_timeline_item($mirror_service, $new_timeline_item,
5050
$_POST['contentType'], file_get_contents($_POST['imageUrl']));
5151
} else {
52-
insertTimelineItem($mirror_service, $new_timeline_item, null, null);
52+
insert_timeline_item($mirror_service, $new_timeline_item, null, null);
5353
}
5454

5555
$message = "Timeline Item inserted!";
5656
break;
57-
case "insertItemWithAction":
57+
case 'insertItemWithAction':
5858
$new_timeline_item = new Google_TimelineItem();
5959
$new_timeline_item->setText("What did you have for lunch?");
6060

6161
$notification = new Google_NotificationConfig();
6262
$notification->setLevel("DEFAULT");
6363
$new_timeline_item->setNotification($notification);
6464

65-
$menuItems = array();
65+
$menu_items = array();
6666

6767
// A couple of built in menu items
68-
$menuItem = new Google_MenuItem();
69-
$menuItem->setAction("READ_ALOUD");
70-
array_push($menuItems, $menuItem);
68+
$menu_item = new Google_MenuItem();
69+
$menu_item->setAction("READ_ALOUD");
70+
array_push($menu_items, $menu_item);
7171
$new_timeline_item->setSpeakableText("What did you eat? Bacon?");
7272

73-
$menuItem = new Google_MenuItem();
74-
$menuItem->setAction("SHARE");
75-
array_push($menuItems, $menuItem);
73+
$menu_item = new Google_MenuItem();
74+
$menu_item->setAction("SHARE");
75+
array_push($menu_items, $menu_item);
7676

7777
// A custom menu item
78-
$customMenuItem = new Google_MenuItem();
79-
$customMenuValue = new Google_MenuValue();
80-
$customMenuValue->setDisplayName("Drill Into");
81-
$customMenuValue->setIconUrl($service_base_url . "/static/images/drill.png");
78+
$custom_menu_item = new Google_MenuItem();
79+
$custom_menu_value = new Google_MenuValue();
80+
$custom_menu_value->setDisplayName("Drill Into");
81+
$custom_menu_value->setIconUrl($service_base_url . "/static/images/drill.png");
8282

83-
$customMenuItem->setValues(array($customMenuValue));
84-
$customMenuItem->setAction("CUSTOM");
83+
$custom_menu_item->setValues(array($custom_menu_value));
84+
$custom_menu_item->setAction("CUSTOM");
8585
// This is how you identify it on the notification ping
86-
$customMenuItem->setId("safe-for-later");
87-
array_push($menuItems, $customMenuItem);
86+
$custom_menu_item->setId("safe-for-later");
87+
array_push($menu_items, $custom_menu_item);
8888

89-
$new_timeline_item->setMenuItems($menuItems);
89+
$new_timeline_item->setMenuItems($menu_items);
9090

91-
insertTimelineItem($mirror_service, $new_timeline_item, null, null);
91+
insert_timeline_item($mirror_service, $new_timeline_item, null, null);
9292

9393
$message = "Inserted a timeline item you can reply to";
9494
break;
95-
case "insertTimelineAllUsers":
95+
case 'insertTimelineAllUsers':
9696
$credentials = list_credentials();
9797
if (count($credentials) > 10) {
9898
$message = "Found " . count($credentials) . " users. Aborting to save your quota.";
@@ -106,25 +106,25 @@
106106

107107
$user_specific_mirror_service = new Google_MirrorService($user_specific_client);
108108

109-
insertTimelineItem($user_specific_mirror_service, $new_timeline_item, null, null);
109+
insert_timeline_item($user_specific_mirror_service, $new_timeline_item, null, null);
110110
}
111111
$message = "Sent a cat fact to " . count($credentials) . " users.";
112112
}
113113
break;
114-
case "insertSubscription":
115-
$message = subscribeToNotifications($mirror_service, $_POST['subscriptionId'],
114+
case 'insertSubscription':
115+
$message = subscribe_to_notifications($mirror_service, $_POST['subscriptionId'],
116116
$_SESSION['userid'], $base_url . "/notify.php");
117117
break;
118-
case "deleteSubscription":
118+
case 'deleteSubscription':
119119
$message = $mirror_service->subscriptions->delete($_POST['subscriptionId']);
120120
break;
121-
case "insertContact":
122-
insertContact($mirror_service, $_POST['id'], $_POST['name'],
121+
case 'insertContact':
122+
insert_contact($mirror_service, $_POST['id'], $_POST['name'],
123123
$base_url . "/static/images/chipotle-tube-640x360.jpg");
124124
$message = "Contact inserted. Enable it on MyGlass.";
125125
break;
126-
case "deleteContact":
127-
deleteContact($mirror_service, $_POST['id']);
126+
case 'deleteContact':
127+
delete_contact($mirror_service, $_POST['id']);
128128
$message = "Contact deleted.";
129129
break;
130130
}
@@ -138,13 +138,13 @@
138138
$contact = null;
139139
}
140140
$subscriptions = $mirror_service->subscriptions->listSubscriptions();
141-
$timelineSubscriptionExists = false;
142-
$locationSubscriptionExists = false;
143-
foreach ($subscriptions['items'] as $subscription) {
144-
if ($subscription['id'] == 'timeline') {
145-
$timelineSubscriptionExists = true;
146-
} elseif ($subscription['id'] == 'location') {
147-
$locationSubscriptionExists = true;
141+
$timeline_subscription_exists = false;
142+
$location_subscription_exists = false;
143+
foreach ($subscriptions->getItems() as $subscription) {
144+
if ($subscription->getId() == 'timeline') {
145+
$timeline_subscription_exists = true;
146+
} elseif ($subscription->getId() == 'location') {
147+
$location_subscription_exists = true;
148148
}
149149
}
150150

@@ -184,26 +184,27 @@
184184
<div class="hero-unit">
185185
<h1>Your Recent Timeline</h1>
186186
<?php if ($message != "") { ?>
187-
<span class="label label-warning">Message: <?= $message ?> </span>
187+
<span class="label label-warning">Message: <?php echo $message; ?> </span>
188188
<?php } ?>
189189

190190
<div style="margin-top: 5px;">
191-
<?php foreach ($timeline['items'] as $timeline_item) { ?>
191+
<?php foreach ($timeline->getItems() as $timeline_item) { ?>
192192
<ul class="span3 tile">
193-
<li><strong>ID: </strong> <?= $timeline_item['id'] ?>
193+
<li><strong>ID: </strong> <?php echo $timeline_item->getId(); ?>
194194
</li>
195195
<li>
196-
<strong>Text: </strong> <?= $timeline_item['text'] ?>
196+
<strong>Text: </strong> <?php echo $timeline_item->getId(); ?>
197197
</li>
198198
<li>
199199
<strong>Attachments: </strong>
200200
<?php
201-
if (isset($timeline_item['attachments'])) {
202-
$attachments = $timeline_item['attachments'];
201+
if ($timeline_item->getAttachments() != null) {
202+
$attachments = $timeline_item->getAttachments();
203203
foreach ($attachments as $attachment) { ?>
204-
<img src="<?= $base_url .
205-
'/attachment-proxy.php?timeline_item_id='.
206-
$timeline_item['id'].'&attachment_id='.$attachment['id'] ?>" />
204+
<img src="<?php echo $base_url .
205+
'/attachment-proxy.php?timeline_item_id=' .
206+
$timeline_item->getId() . '&attachment_id=' .
207+
$attachment->getId() ?>" />
207208
<?php
208209
}
209210
}
@@ -236,12 +237,12 @@
236237
<input type="hidden" name="operation" value="insertItem">
237238
<input type="hidden" name="message"
238239
value="Chipotle says hi!">
239-
<input type="hidden" name="imageUrl" value="<?= $base_url .
240+
<input type="hidden" name="imageUrl" value="<?php echo $base_url .
240241
"/static/images/chipotle-tube-640x360.jpg" ?>">
241242
<input type="hidden" name="contentType" value="image/jpeg">
242243

243244
<button class="btn" type="submit">A picture
244-
<img class="button-icon" src="<?= $base_url .
245+
<img class="button-icon" src="<?php echo $base_url .
245246
"/static/images/chipotle-tube-640x360.jpg" ?>">
246247
</button>
247248
</form>
@@ -265,7 +266,7 @@
265266
<?php if ($contact == null) { ?>
266267
<form class="span3"method="post">
267268
<input type="hidden" name="operation" value="insertContact">
268-
<input type="hidden" name="iconUrl" value="<?= $base_url .
269+
<input type="hidden" name="iconUrl" value="<?php echo $base_url .
269270
"/static/images/chipotle-tube-640x360.jpg" ?>">
270271
<input type="hidden" name="name" value="PHP Quick Start">
271272
<input type="hidden" name="id" value="php-quick-start">
@@ -277,7 +278,7 @@
277278
<input type="hidden" name="id" value="php-quick-start">
278279
<button class="btn" type="submit">Delete PHP Quick Start Contact</button>
279280
</form>
280-
<? } ?>
281+
<?php } ?>
281282
</div>
282283

283284
<div class="span4">
@@ -290,22 +291,22 @@
290291
<p class="label label-info">Note: Subscriptions require SSL. <br>They will
291292
not work on localhost.</p>
292293

293-
<?php if ($timelineSubscriptionExists) { ?>
294+
<?php if ($timeline_subscription_exists) { ?>
294295
<form method="post">
295296
<input type="hidden" name="subscriptionId" value="timeline">
296297
<input type="hidden" name="operation" value="deleteSubscription">
297298
<button class="btn" type="submit">Unsubscribe from
298299
timeline updates</button>
299300
</form>
300-
<? } else { ?>
301+
<?php } else { ?>
301302
<form method="post">
302303
<input type="hidden" name="operation" value="insertSubscription">
303304
<input type="hidden" name="subscriptionId" value="timeline">
304305
<button class="btn" type="submit">Subscribe to timeline updates</button>
305306
</form>
306307
<?php } ?>
307308

308-
<?php if ($locationSubscriptionExists) { ?>
309+
<?php if ($location_subscription_exists) { ?>
309310
<form method="post">
310311
<input type="hidden" name="subscriptionId" value="location">
311312
<input type="hidden" name="operation" value="deleteSubscription">

mirror-client.php

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,41 @@
2323

2424
require_once 'config.php';
2525

26-
function insertTimelineItem($service, $timelineItem, $contentType, $attachment)
26+
// Returns an unauthenticated service
27+
function get_google_api_client() {
28+
global $api_client_id, $api_client_secret, $api_simple_key, $base_url;
29+
// Set your cached access token. Remember to replace $_SESSION with a
30+
// real database or memcached.
31+
session_start();
32+
33+
$client = new Google_Client();
34+
35+
$client->setUseObjects(true);
36+
$client->setApplicationName('Google Mirror API PHP Quick Start');
37+
38+
// These are set in config.php
39+
$client->setClientId($api_client_id);
40+
$client->setClientSecret($api_client_secret);
41+
$client->setDeveloperKey($api_simple_key);
42+
$client->setRedirectUri($base_url."/oauth2callback.php");
43+
44+
$client->setScopes(array(
45+
'https://www.googleapis.com/auth/glass.timeline',
46+
'https://www.googleapis.com/auth/glass.location',
47+
'https://www.googleapis.com/auth/userinfo.profile'));
48+
49+
return $client;
50+
}
51+
52+
function insert_timeline_item($service, $timeline_item, $content_type, $attachment)
2753
{
2854
try {
29-
$optParams = array();
30-
if ($contentType != null && $attachment != null) {
31-
$optParams['data'] = $attachment;
32-
$optParams['mimeType'] = $contentType;
55+
$opt_params = array();
56+
if ($content_type != null && $attachment != null) {
57+
$opt_params['data'] = $attachment;
58+
$opt_params['mimeType'] = $content_type;
3359
}
34-
return $service->timeline->insert($timelineItem, $optParams);
60+
return $service->timeline->insert($timeline_item, $opt_params);
3561
} catch (Exception $e) {
3662
print 'An error ocurred: ' . $e->getMessage();
3763
return null;
@@ -44,32 +70,32 @@ function insertTimelineItem($service, $timelineItem, $contentType, $attachment)
4470
* @param Google_MirrorService $service Authorized Mirror service.
4571
* @param string $collection Collection to subscribe to (supported
4672
* values are "timeline" and "locations").
47-
* @param string $userToken Opaque token used by the Service to
73+
* @param string $user_token Opaque token used by the Service to
4874
* identify the user the notification pings
4975
* are sent for (recommended).
50-
* @param string $callbackUrl URL receiving notification pings (must be HTTPS).
76+
* @param string $callback_url URL receiving notification pings (must be HTTPS).
5177
*/
52-
function subscribeToNotifications($service, $collection, $userToken, $callbackUrl)
78+
function subscribe_to_notifications($service, $collection, $user_token, $callback_url)
5379
{
5480
try {
5581
$subscription = new Google_Subscription();
5682
$subscription->setCollection($collection);
57-
$subscription->setUserToken($userToken);
58-
$subscription->setCallbackUrl($callbackUrl);
83+
$subscription->setUserToken($user_token);
84+
$subscription->setCallbackUrl($callback_url);
5985
$service->subscriptions->insert($subscription);
6086
return "Subscription inserted!";
6187
} catch (Exception $e) {
6288
return 'An error occurred: ' . $e->getMessage();
6389
}
6490
}
6591

66-
function insertContact($service, $contactId, $displayName, $iconUrl)
92+
function insert_contact($service, $contact_id, $display_name, $icon_url)
6793
{
6894
try {
6995
$contact = new Google_Contact();
70-
$contact->setId($contactId);
71-
$contact->setDisplayName($displayName);
72-
$contact->setImageUrls(array($iconUrl));
96+
$contact->setId($contact_id);
97+
$contact->setDisplayName($display_name);
98+
$contact->setImageUrls(array($icon_url));
7399
return $service->contacts->insert($contact);
74100
} catch (Exception $e) {
75101
print 'An error ocurred: ' . $e->getMessage();
@@ -81,11 +107,11 @@ function insertContact($service, $contactId, $displayName, $iconUrl)
81107
* Delete a contact for the current user.
82108
*
83109
* @param Google_MirrorService $service Authorized Mirror service.
84-
* @param string $contactId ID of the Contact to delete.
110+
* @param string $contact_id ID of the Contact to delete.
85111
*/
86-
function deleteContact($service, $contactId) {
112+
function delete_contact($service, $contact_id) {
87113
try {
88-
$service->contacts->delete($contactId);
114+
$service->contacts->delete($contact_id);
89115
} catch (Exception $e) {
90116
print 'An error occurred: ' . $e->getMessage();
91117
}
@@ -94,12 +120,12 @@ function deleteContact($service, $contactId) {
94120
/**
95121
* Download an attachment's content.
96122
*
97-
* @param string $timelineId ID of the timeline item the attachment belongs to.
123+
* @param string item_id ID of the timeline item the attachment belongs to.
98124
* @param Google_Attachment $attachment Attachment's metadata.
99125
* @return string The attachment's content if successful, null otherwise.
100126
*/
101-
function downloadAttachment($itemId, $attachment) {
102-
$request = new Google_HttpRequest($attachment['contentUrl'], 'GET', null, null);
127+
function download_attachment($item_id, $attachment) {
128+
$request = new Google_HttpRequest($attachment->getContentUrl(), 'GET', null, null);
103129
$httpRequest = Google_Client::$io->authenticatedRequest($request);
104130
if ($httpRequest->getResponseHttpCode() == 200) {
105131
return $httpRequest->getResponseBody();

0 commit comments

Comments
 (0)