Skip to content
This repository has been archived by the owner on Oct 4, 2019. It is now read-only.

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
google-automerger committed Nov 18, 2014
0 parents commit 52b9f14
Show file tree
Hide file tree
Showing 40 changed files with 1,668 additions and 0 deletions.
71 changes: 71 additions & 0 deletions Application/build.gradle
@@ -0,0 +1,71 @@
buildscript {
repositories {
mavenCentral()
}

dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
}
}

apply plugin: 'com.android.application'


dependencies {

compile "com.android.support:support-v4:21.+"
compile "com.android.support:support-v13:21.+"
compile "com.android.support:cardview-v7:21.+"

}

// The sample build uses multiple directories to
// keep boilerplate and common code separate from
// the main sample code.
List<String> dirs = [
'main', // main sample code; look here for the interesting stuff.
'common', // components that are reused by multiple samples
'template'] // boilerplate code that is generated by the sample template process

android {
compileSdkVersion 21
buildToolsVersion "21.0.0"

defaultConfig {
minSdkVersion 21
targetSdkVersion 21
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}

sourceSets {
main {
dirs.each { dir ->
java.srcDirs "src/${dir}/java"
res.srcDirs "src/${dir}/res"
}
}
androidTest.setRoot('tests')
androidTest.java.srcDirs = ['tests/src']

}

}















52 changes: 52 additions & 0 deletions Application/src/main/AndroidManifest.xml
@@ -0,0 +1,52 @@
<!--
Copyright (C) 2014 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.messagingservice">

<application android:allowBackup="true"
android:label="@string/app_name"
android:icon="@drawable/ic_launcher"
android:theme="@style/AppTheme">

<meta-data android:name="com.google.android.gms.car.application"
android:resource="@xml/automotive_app_desc"/>

<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service android:name=".MessagingService">
</service>

<receiver android:name=".MessageReadReceiver">
<intent-filter>
<action android:name="com.example.android.messagingservice.ACTION_MESSAGE_READ"/>
</intent-filter>
</receiver>

<receiver android:name=".MessageReplyReceiver">
<intent-filter>
<action android:name="com.example.android.messagingservice.ACTION_MESSAGE_REPLY"/>
</intent-filter>
</receiver>
</application>
</manifest>
@@ -0,0 +1,126 @@
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.android.messagingservice;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

/**
* A simple class that denotes unread conversations and messages. In a real world application,
* this would be replaced by a content provider that actually gets the unread messages to be
* shown to the user.
*/
public class Conversations {

/**
* Set of strings used as messages by the sample.
*/
private static final String[] MESSAGES = new String[]{
"Are you at home?",
"Can you give me a call?",
"Hey yt?",
"Don't forget to get some milk on your way back home",
"Is that project done?",
"Did you finish the Messaging app yet?"
};

/**
* Senders of the said messages.
*/
private static final String[] PARTICIPANTS = new String[]{
"John Smith",
"Robert Lawrence",
"James Smith",
"Jane Doe"
};

static class Conversation {

private final int conversationId;

private final String participantName;

/**
* A given conversation can have a single or multiple messages.
* Note that the messages are sorted from *newest* to *oldest*
*/
private final List<String> messages;

private final long timestamp;

public Conversation(int conversationId, String participantName,
List<String> messages) {
this.conversationId = conversationId;
this.participantName = participantName;
this.messages = messages == null ? Collections.<String>emptyList() : messages;
this.timestamp = System.currentTimeMillis();
}

public int getConversationId() {
return conversationId;
}

public String getParticipantName() {
return participantName;
}

public List<String> getMessages() {
return messages;
}

public long getTimestamp() {
return timestamp;
}

public String toString() {
return "[Conversation: conversationId=" + conversationId +
", participantName=" + participantName +
", messages=" + messages +
", timestamp=" + timestamp + "]";
}
}

private Conversations() {
}

public static Conversation[] getUnreadConversations(int howManyConversations,
int messagesPerConversation) {
Conversation[] conversations = new Conversation[howManyConversations];
for (int i = 0; i < howManyConversations; i++) {
conversations[i] = new Conversation(
ThreadLocalRandom.current().nextInt(),
name(), makeMessages(messagesPerConversation));
}
return conversations;
}

private static List<String> makeMessages(int messagesPerConversation) {
int maxLen = MESSAGES.length;
List<String> messages = new ArrayList<>(messagesPerConversation);
for (int i = 0; i < messagesPerConversation; i++) {
messages.add(MESSAGES[ThreadLocalRandom.current().nextInt(0, maxLen)]);
}
return messages;
}

private static String name() {
return PARTICIPANTS[ThreadLocalRandom.current().nextInt(0, PARTICIPANTS.length)];
}
}
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.android.messagingservice;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new MessagingFragment())
.commit();
}
}
}
@@ -0,0 +1,57 @@
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.android.messagingservice;

import android.content.Context;
import android.content.SharedPreferences;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
* A simple logger that uses shared preferences to log messages, their reads
* and replies. Don't use this in a real world application. This logger is only
* used for displaying the messages in the text view.
*/
public class MessageLogger {

private static final String PREF_MESSAGE = "MESSAGE_LOGGER";
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

public static final String LOG_KEY = "message_data";
public static final String LINE_BREAKS = "\n\n";

public static void logMessage(Context context, String message) {
SharedPreferences prefs = getPrefs(context);
message = DATE_FORMAT.format(new Date(System.currentTimeMillis())) + ": " + message;
prefs.edit()
.putString(LOG_KEY, prefs.getString(LOG_KEY, "") + LINE_BREAKS + message)
.apply();
}

public static SharedPreferences getPrefs(Context context) {
return context.getSharedPreferences(PREF_MESSAGE, Context.MODE_PRIVATE);
}

public static String getAllMessages(Context context) {
return getPrefs(context).getString(LOG_KEY, "");
}

public static void clear(Context context) {
getPrefs(context).edit().remove(LOG_KEY).apply();
}
}
@@ -0,0 +1,42 @@
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.android.messagingservice;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationManagerCompat;
import android.util.Log;

public class MessageReadReceiver extends BroadcastReceiver {
private static final String TAG = MessageReadReceiver.class.getSimpleName();

private static final String CONVERSATION_ID = "conversation_id";

@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive");
int conversationId = intent.getIntExtra(CONVERSATION_ID, -1);
if (conversationId != -1) {
Log.d(TAG, "Conversation " + conversationId + " was read");
MessageLogger.logMessage(context, "Conversation " + conversationId + " was read.");
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.cancel(conversationId);
}
}
}

0 comments on commit 52b9f14

Please sign in to comment.