Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Render/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
build/
.idea
.gradle
*.iml
local.properties
.DS_Store
app/libs
1 change: 1 addition & 0 deletions Render/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/
26 changes: 26 additions & 0 deletions Render/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 21
buildToolsVersion "21.1.2"

defaultConfig {
applicationId "com.example.android.mobileperf.render"
minSdkVersion 9
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.0'
compile 'com.squareup.picasso:picasso:2.2.0'
}
17 changes: 17 additions & 0 deletions Render/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/shailentuli/Library/Android/sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
25 changes: 25 additions & 0 deletions Render/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.mobileperf.render" >

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.Base" >
<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>
<activity
android:name=".ChatumLatinumActivity"
android:label="@string/title_activity_chatum_latinum" >
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.mobileperf.render;

import java.util.Date;

/**
* Describes a Chat message written by a Droid author, consisting of some text and a timestamp.
*/
public class Chat {
/**
* The author of the chat.
*/
private Droid author;

/**
* The chat content. This is hardcoded in this sample (see ChatsFragment.java).
*/
private String text;

/**
* The time a chat was posted to the chat board.
*/
private Date datetime;

public Chat(Droid author, String text, Date datetime) {
this.author = author;
this.text = text;
this.datetime = datetime;
}

public Droid getAuthor() {return author;}
public String getText() {return text;}
public Date getDatetime() {return datetime;}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* 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.mobileperf.render;

import android.content.Context;
import android.graphics.Color;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.Date;

/**
* A custom adapter that is backed by an array of Chat objects. References a TextView with the name
* of a chat author (a Droid), a TextView with a chat's text, another TextView with the chat's
* timestamp, and an ImageView for the chat author's avatar.
*/
public class ChatAdapter extends ArrayAdapter<Chat> {
public ChatAdapter(Context context, ArrayList<Chat> chats) {
super(context, 0, chats);
}

@Override
public View getView(int position, View view, ViewGroup parent) {

Chat chat = getItem(position);
if (view == null) {
view = LayoutInflater.from(getContext()).inflate(
R.layout.chat_item, parent, false);
}

// Find the UI widgets for a chat item.
TextView chat_author_name = (TextView) view.findViewById(R.id.chat_author_name);
TextView chat_text = (TextView) view.findViewById(R.id.chat_text);
TextView chat_datetime = (TextView) view.findViewById(R.id.chat_datetime);
ImageView chat_author_avatar = (ImageView) view.findViewById(R.id.chat_author_avatar);

// Display the author's name using the color associated with the author.
chat_author_name.setText(chat.getAuthor().getName());
chat_author_name.setTextColor(chat.getAuthor().getColor());

// Display the chat text.
chat_text.setText(chat.getText());

// Set the timestamp for the chat in "x minutes ago" format.
chat_datetime.setText(DateUtils.getRelativeTimeSpanString(
chat.getDatetime().getTime(),
new Date().getTime(),
DateUtils.MINUTE_IN_MILLIS,
DateUtils.FORMAT_ABBREV_RELATIVE));


// Display the chat author's avatar (a droid image) and a background color associated with
// the author.
if (chat.getAuthor().getAvatarId() != 0) {
Picasso.with(getContext()).load(chat.getAuthor().getAvatarId()).into(
chat_author_avatar);
}
chat_author_avatar.setBackgroundColor(chat.getAuthor().getColor());

return view;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* 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.mobileperf.render;

import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Date;


/**
* Fragment that encapsulates creating {@link Chat} objects and displaying them using a
* {@link ListView} layout.
* Creates seed data consisting of a handful of chats authored by a few authors.
*/
public class ChatsFragment extends Fragment {
protected static int MILLISECONDS_PER_SECOND = 1000;
protected static int SECONDS_PER_MINUTE = 60;

public ChatsFragment() {}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

// Create a list of chats and populate it with hardcoded data.
ArrayList<Chat> chats = new ArrayList<Chat>();
populateChats(chats);

// Create the adapter that provides values to the UI Widgets.
ChatAdapter adapter = new ChatAdapter(getActivity(), chats);

View rootView = inflater.inflate(R.layout.fragment_chats, container, false);

// Find the ListView that holds all the chat messages and attach it to the adapter,
ListView listView = (ListView) rootView.findViewById(R.id.listview_chats);
listView.setAdapter(adapter);

return rootView;
}

private Date getTimeInPast(int minutesAgo) {
return new Date(new Date().getTime() -
(minutesAgo * SECONDS_PER_MINUTE * MILLISECONDS_PER_SECOND));
}

// Creates hardcoded chat objects.
private void populateChats(ArrayList<Chat> chats) {
Resources res = getResources();
Droid alex = new Droid("alex", res.getColor(R.color.alex_color));
Droid joanna = new Droid("joanna", res.getColor(R.color.joanna_color), R.drawable.joanna);
Droid shailen = new Droid("shailen", res.getColor(R.color.shailen_color),
R.drawable.shailen);

chats.add(new Chat(alex, "Lorem ipsum dolor sit amet, orci nullam cra",
getTimeInPast(15)));

chats.add(new Chat(joanna, "Omnis aptent magnis suspendisse ipsum, semper egestas " +
"magna auctor maecenas",
getTimeInPast(11)));

chats.add(new Chat(shailen, "eu nibh, rhoncus wisi posuere lacus, ad erat egestas " +
"quam, magna ante ultricies sem",
getTimeInPast(9)));

chats.add(new Chat(alex, "rhoncus wisi posuere lacus, ad erat egestas quam, magna " +
"ante ultricies sem lacus",
getTimeInPast(8)));

chats.add(new Chat(shailen, "Enim justo nisl sit proin, quis vestibulum vivamus " +
"suscipit penatibus et id, tempus mauris a lacus blandit, aenean praesent " +
"arcu scelerisque sociosqu. Nonummy at ut ullamcorper nulla, ligula id, " +
"nullam donec nisl ante turpis duis mauris, dolor imperdiet a inceptos aliquam",
getTimeInPast(8)));

chats.add(new Chat(joanna, "Omnis aptent magnis.",
getTimeInPast(7)));

chats.add(new Chat(alex, "Metus tincidunt sit in urna.",
getTimeInPast(6)));

chats.add(new Chat(shailen, "Non blandit nulla dapibus, vitae quisque sed cras mi " +
"leo condimentum sociosqu quis sed pharetra",
getTimeInPast(4)));

chats.add(new Chat(joanna, "Enim justo nisl sit proin, quis vestibulum vivamus " +
"suscipit penatibus et id, tempus mauris a lacus blandit, aenean praesent " +
"arcu scelerisque sociosqu. Nonummy at ut ullamcorper nulla, ligula id, " +
"nullam donec nisl ante turpis duis mauris, dolor imperdiet a inceptos.",
getTimeInPast(3)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.mobileperf.render;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;

/**
* Displays a fragment that contains a ListView holding several Chat objects. Used in this lesson
* to show a) how to reduce overdraws, and b) how to flatten needlessly nested hierarchies.
*/
public class ChatumLatinumActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chatum_latinum);

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.activity_chatum_latinum_container, new ChatsFragment())
.commit();
}
}
}
Loading