Skip to content

Commit 94011e7

Browse files
committed
Add GCM to server
Signed-off-by: Jonas Kalderstam <[email protected]>
1 parent b9f8624 commit 94011e7

File tree

15 files changed

+618
-166
lines changed

15 files changed

+618
-166
lines changed

android-client/AndroidManifest.xml

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@
2020
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
2121
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
2222

23+
<!-- For GCM -->
24+
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
25+
<!-- Keeps the processor from sleeping when a message is received. -->
26+
<uses-permission android:name="android.permission.WAKE_LOCK" />
27+
28+
<permission
29+
android:name="com.nononsenseapps.linksgcm.permission.C2D_MESSAGE"
30+
android:protectionLevel="signature" />
31+
32+
<uses-permission android:name="com.nononsenseapps.linksgcm.permission.C2D_MESSAGE" />
33+
2334
<application
2435
android:allowBackup="true"
2536
android:icon="@drawable/ic_launcher"
@@ -70,7 +81,27 @@
7081
android:name="android.content.SyncAdapter"
7182
android:resource="@xml/syncadapter" />
7283
</service>
73-
84+
85+
<receiver
86+
android:name="com.nononsenseapps.linksgcm.gcm.GCMReceiver"
87+
android:enabled="true"
88+
android:exported="true"
89+
android:permission="com.google.android.c2dm.permission.SEND" >
90+
<intent-filter>
91+
92+
<!-- Receives the actual messages. -->
93+
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
94+
<!-- Receives the registration id. -->
95+
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
96+
97+
<category android:name="com.nononsenseapps.linksgcm" />
98+
</intent-filter>
99+
</receiver>
100+
101+
<service
102+
android:name="com.nononsenseapps.linksgcm.gcm.GCMIntentService"
103+
android:exported="false" >
104+
</service>
74105
</application>
75106

76107
</manifest>

android-client/src/com/nononsenseapps/linksgcm/database/DatabaseHandler.java

Lines changed: 144 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -11,158 +11,152 @@
1111

1212
/**
1313
* Database handler, SQLite wrapper and ORM layer.
14-
*
14+
*
1515
*/
1616
public class DatabaseHandler extends SQLiteOpenHelper {
1717

18-
// All Static variables
19-
// Database Version
20-
private static final int DATABASE_VERSION = 1;
21-
22-
// Database Name
23-
private static final String DATABASE_NAME = "SampleDB";
24-
private final Context context;
25-
26-
private static DatabaseHandler instance = null;
27-
28-
public synchronized static DatabaseHandler getInstance(Context context) {
29-
if (instance == null)
30-
instance = new DatabaseHandler(context.getApplicationContext());
31-
return instance;
32-
}
33-
34-
public DatabaseHandler(Context context) {
35-
super(context.getApplicationContext(), DATABASE_NAME, null,
36-
DATABASE_VERSION);
37-
this.context = context.getApplicationContext();
38-
}
39-
40-
@Override
41-
public void onOpen(SQLiteDatabase db) {
42-
super.onOpen(db);
43-
if (!db.isReadOnly()) {
44-
// Enable foreign key constraints
45-
// This line requires android16
46-
// db.setForeignKeyConstraintsEnabled(true);
47-
// This line works everywhere though
48-
db.execSQL("PRAGMA foreign_keys=ON;");
49-
50-
// Create temporary triggers
51-
DatabaseTriggers.createTemp(db);
52-
}
53-
}
54-
55-
@Override
56-
public synchronized void onCreate(SQLiteDatabase db) {
57-
58-
db.execSQL("DROP TABLE IF EXISTS " + LinkItem.TABLE_NAME);
59-
db.execSQL(LinkItem.CREATE_TABLE);
60-
61-
62-
// Create Triggers
63-
DatabaseTriggers.create(db);
64-
}
65-
66-
// Upgrading database
67-
@Override
68-
public synchronized void onUpgrade(SQLiteDatabase db, int oldVersion,
69-
int newVersion) {
70-
// Try to drop and recreate. You should do something clever here
71-
onCreate(db);
72-
}
73-
74-
// Convenience methods
75-
public synchronized boolean putItem(final DBItem item) {
76-
boolean success = false;
77-
int result = 0;
78-
final SQLiteDatabase db = this.getWritableDatabase();
79-
final ContentValues values = item.getContent();
80-
81-
if (item.getId() > -1) {
82-
result += db.update(item.getTableName(), values,
83-
DBItem.COL_ID + " IS ?",
84-
new String[] { String.valueOf(item.getId()) });
85-
}
86-
87-
// Update failed or wasn't possible, insert instead
88-
if (result < 1) {
89-
final long id = db.insert(item.getTableName(), null, values);
90-
91-
if (id > 0) {
92-
item.setId(id);
93-
success = true;
94-
}
95-
}
96-
else {
97-
success = true;
98-
}
99-
100-
if (success) {
101-
item.notifyProvider(context);
102-
}
103-
return success;
104-
}
105-
106-
public synchronized int deleteItem(LinkItem item) {
107-
final SQLiteDatabase db = this.getWritableDatabase();
108-
final int result = db.delete(item.getTableName(), LinkItem.COL_SHA
109-
+ " IS ?", new String[] { item.sha });
110-
111-
if (result > 0) {
112-
item.notifyProvider(context);
113-
}
114-
115-
return result;
116-
}
117-
118-
119-
public synchronized Cursor getLinkItemCursor(final long id) {
120-
final SQLiteDatabase db = this.getReadableDatabase();
121-
final Cursor cursor = db.query(LinkItem.TABLE_NAME,
122-
LinkItem.FIELDS, LinkItem.COL_ID + " IS ?",
123-
new String[] { String.valueOf(id) }, null, null, null, null);
124-
return cursor;
125-
}
126-
127-
public synchronized LinkItem getLinkItem(final long id) {
128-
final Cursor cursor = getLinkItemCursor(id);
129-
final LinkItem result;
130-
if (cursor.moveToFirst()) {
131-
result = new LinkItem(cursor);
132-
}
133-
else {
134-
result = null;
135-
}
136-
137-
cursor.close();
138-
return result;
139-
}
140-
141-
public synchronized Cursor getAllLinkItemsCursor(final String selection,
142-
final String[] args,
143-
final String sortOrder) {
144-
final SQLiteDatabase db = this.getReadableDatabase();
145-
146-
final Cursor cursor = db.query(LinkItem.TABLE_NAME,
147-
LinkItem.FIELDS, selection, args, null, null, sortOrder, null);
148-
149-
return cursor;
150-
}
151-
152-
public synchronized List<LinkItem> getAllLinkItems(final String selection,
153-
final String[] args,
154-
final String sortOrder) {
155-
final List<LinkItem> result = new ArrayList<LinkItem>();
156-
157-
final Cursor cursor = getAllLinkItemsCursor(selection, args, sortOrder);
158-
159-
while (cursor.moveToNext()) {
160-
LinkItem q = new LinkItem(cursor);
161-
result.add(q);
162-
}
163-
164-
cursor.close();
165-
return result;
166-
}
18+
// All Static variables
19+
// Database Version
20+
private static final int DATABASE_VERSION = 1;
21+
22+
// Database Name
23+
private static final String DATABASE_NAME = "SampleDB";
24+
private final Context context;
25+
26+
private static DatabaseHandler instance = null;
27+
28+
public synchronized static DatabaseHandler getInstance(Context context) {
29+
if (instance == null)
30+
instance = new DatabaseHandler(context.getApplicationContext());
31+
return instance;
32+
}
33+
34+
public DatabaseHandler(Context context) {
35+
super(context.getApplicationContext(), DATABASE_NAME, null,
36+
DATABASE_VERSION);
37+
this.context = context.getApplicationContext();
38+
}
39+
40+
@Override
41+
public void onOpen(SQLiteDatabase db) {
42+
super.onOpen(db);
43+
if (!db.isReadOnly()) {
44+
// Enable foreign key constraints
45+
// This line requires android16
46+
// db.setForeignKeyConstraintsEnabled(true);
47+
// This line works everywhere though
48+
db.execSQL("PRAGMA foreign_keys=ON;");
49+
50+
// Create temporary triggers
51+
DatabaseTriggers.createTemp(db);
52+
}
53+
}
54+
55+
@Override
56+
public synchronized void onCreate(SQLiteDatabase db) {
57+
58+
db.execSQL("DROP TABLE IF EXISTS " + LinkItem.TABLE_NAME);
59+
db.execSQL(LinkItem.CREATE_TABLE);
60+
61+
// Create Triggers
62+
DatabaseTriggers.create(db);
63+
}
64+
65+
// Upgrading database
66+
@Override
67+
public synchronized void onUpgrade(SQLiteDatabase db, int oldVersion,
68+
int newVersion) {
69+
// Try to drop and recreate. You should do something clever here
70+
onCreate(db);
71+
}
72+
73+
// Convenience methods
74+
public synchronized boolean putItem(final LinkItem item) {
75+
boolean success = false;
76+
int result = 0;
77+
final SQLiteDatabase db = this.getWritableDatabase();
78+
final ContentValues values = item.getContent();
79+
80+
if (item.getId() > -1) {
81+
result += db.update(item.getTableName(), values, DBItem.COL_ID
82+
+ " IS ?", new String[] { String.valueOf(item.getId()) });
83+
}
84+
// Update failed or wasn't possible, insert instead
85+
else {
86+
final long id = db.insert(item.getTableName(), null, values);
87+
if (id > 0) {
88+
item.setId(id);
89+
result++;
90+
}
91+
}
92+
93+
if (result > 0) {
94+
success = true;
95+
}
96+
if (success) {
97+
item.notifyProvider(context);
98+
}
99+
return success;
100+
}
101+
102+
public synchronized int deleteItem(LinkItem item) {
103+
final SQLiteDatabase db = this.getWritableDatabase();
104+
final int result = db.delete(item.getTableName(), LinkItem.COL_ID
105+
+ " IS ? OR " + LinkItem.COL_SHA + " IS ?",
106+
new String[] { Long.toString(item._id), item.sha });
107+
108+
if (result > 0) {
109+
item.notifyProvider(context);
110+
}
111+
112+
return result;
113+
}
114+
115+
public synchronized Cursor getLinkItemCursor(final long id) {
116+
final SQLiteDatabase db = this.getReadableDatabase();
117+
final Cursor cursor = db.query(LinkItem.TABLE_NAME, LinkItem.FIELDS,
118+
LinkItem.COL_ID + " IS ?", new String[] { String.valueOf(id) },
119+
null, null, null, null);
120+
return cursor;
121+
}
122+
123+
public synchronized LinkItem getLinkItem(final long id) {
124+
final Cursor cursor = getLinkItemCursor(id);
125+
final LinkItem result;
126+
if (cursor.moveToFirst()) {
127+
result = new LinkItem(cursor);
128+
}
129+
else {
130+
result = null;
131+
}
132+
133+
cursor.close();
134+
return result;
135+
}
136+
137+
public synchronized Cursor getAllLinkItemsCursor(final String selection,
138+
final String[] args, final String sortOrder) {
139+
final SQLiteDatabase db = this.getReadableDatabase();
140+
141+
final Cursor cursor = db.query(LinkItem.TABLE_NAME, LinkItem.FIELDS,
142+
selection, args, null, null, sortOrder, null);
143+
144+
return cursor;
145+
}
146+
147+
public synchronized List<LinkItem> getAllLinkItems(final String selection,
148+
final String[] args, final String sortOrder) {
149+
final List<LinkItem> result = new ArrayList<LinkItem>();
150+
151+
final Cursor cursor = getAllLinkItemsCursor(selection, args, sortOrder);
152+
153+
while (cursor.moveToNext()) {
154+
LinkItem q = new LinkItem(cursor);
155+
result.add(q);
156+
}
157+
158+
cursor.close();
159+
return result;
160+
}
167161

168162
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.nononsenseapps.linksgcm.gcm;
2+
3+
public class GCMConfig {
4+
5+
public static final String SENDER_ID = "86425096293";
6+
}

0 commit comments

Comments
 (0)