Skip to content

Commit cc92c10

Browse files
author
Roger Hu
committed
Lecture 3 demo.
0 parents  commit cc92c10

27 files changed

+788
-0
lines changed

.gitignore

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Source: https://gist.github.com/nesquena/5617544/raw/53710b374e7df3302df43b552488d876040ada3d/.gitignore
2+
3+
# built application files
4+
*.apk
5+
*.ap_
6+
7+
# files for the dex VM
8+
*.dex
9+
10+
# Java class files
11+
*.class
12+
13+
# generated files
14+
bin/
15+
gen/
16+
17+
# Local configuration file (sdk path, etc)
18+
local.properties
19+
20+
# Eclipse project files
21+
.classpath
22+
.project
23+
24+
# Proguard folder generated by Eclipse
25+
proguard/
26+
proguard-project.txt
27+
28+
# Intellij project files
29+
*.iml
30+
*.ipr
31+
*.iws
32+
.idea/
33+
34+
*.pydevproject
35+
.project
36+
.metadata
37+
bin/**
38+
tmp/**
39+
tmp/**/*
40+
*.tmp
41+
*.bak
42+
*.swp
43+
*~.nib
44+
local.properties
45+
.classpath
46+
.settings/
47+
.loadpath
48+
49+
# External tool builders
50+
.externalToolBuilders/
51+
52+
# Locally stored "Eclipse launch configurations"
53+
*.launch
54+
55+
# CDT-specific
56+
.cproject
57+
58+
# PDT-specific
59+
.buildpath
60+
61+
# Android Studio project files
62+
*.iml
63+
.gradle
64+
.idea
65+
build
66+
import-summary.txt

app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/build.gradle

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 21
5+
buildToolsVersion "21.1.2"
6+
7+
defaultConfig {
8+
applicationId "codepath.com.myfun"
9+
minSdkVersion 16
10+
targetSdkVersion 21
11+
versionCode 1
12+
versionName "1.0"
13+
}
14+
buildTypes {
15+
release {
16+
minifyEnabled false
17+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18+
}
19+
}
20+
}
21+
22+
dependencies {
23+
compile fileTree(dir: 'libs', include: ['*.jar'])
24+
compile 'com.android.support:appcompat-v7:21.0.3'
25+
}

app/proguard-rules.pro

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /Users/rhu/Library/Android/sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package codepath.com.myfun;
2+
3+
import android.app.Application;
4+
import android.test.ApplicationTestCase;
5+
6+
/**
7+
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
8+
*/
9+
public class ApplicationTest extends ApplicationTestCase<Application> {
10+
public ApplicationTest() {
11+
super(Application.class);
12+
}
13+
}

app/src/main/AndroidManifest.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="codepath.com.myfun" >
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@drawable/ic_launcher"
8+
android:label="@string/app_name"
9+
android:theme="@style/AppTheme" >
10+
<activity
11+
android:name=".MainActivity"
12+
android:label="@string/app_name" >
13+
<intent-filter>
14+
<action android:name="android.intent.action.MAIN" />
15+
16+
<category android:name="android.intent.category.LAUNCHER" />
17+
</intent-filter>
18+
</activity>
19+
</application>
20+
<uses-permission android:name="android.permission.INTERNET"/>
21+
</manifest>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package codepath.com.myfun;
2+
3+
public class ImageLinkCount {
4+
private long id;
5+
private String URL;
6+
private int timesSeen;
7+
8+
public ImageLinkCount(String URL, int timesSeen) {
9+
setURL(URL);
10+
setTimesSeen(timesSeen);
11+
}
12+
public long getId() {
13+
return id;
14+
}
15+
16+
public void setId(long id) {
17+
this.id = id;
18+
}
19+
20+
public String getURL() {
21+
return URL;
22+
}
23+
24+
public void setURL(String URL) {
25+
this.URL = URL;
26+
}
27+
28+
public int getTimesSeen() {
29+
return timesSeen;
30+
}
31+
32+
public void setTimesSeen(int timesSeen) {
33+
this.timesSeen = timesSeen;
34+
}
35+
36+
public void incrementTimesSeen() {
37+
this.timesSeen += 1;
38+
}
39+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package codepath.com.myfun;
2+
3+
import android.content.ContentValues;
4+
import android.content.Context;
5+
import android.database.Cursor;
6+
import android.database.sqlite.SQLiteDatabase;
7+
import android.database.sqlite.SQLiteOpenHelper;
8+
import android.util.Log;
9+
10+
public class ImageLinksCountDatabase extends SQLiteOpenHelper {
11+
12+
private static final int DATABASE_VERSION = 1;
13+
14+
private static final String DATABASE_NAME = "abc";
15+
16+
private static final String TABLE_LINKS = "link_items";
17+
18+
private static final String KEY_ID = "id";
19+
private static final String KEY_URL = "url";
20+
private static final String KEY_TIMES_SEEN = "times_seen";
21+
22+
ImageLinksCountDatabase(Context context) {
23+
super(context, DATABASE_NAME, null, DATABASE_VERSION);
24+
}
25+
26+
@Override
27+
public void onCreate(SQLiteDatabase db) {
28+
String CREATE_ITEMS_TABLE = String.format("CREATE TABLE %s (%s INTEGER PRIMARY KEY, %s TEXT, %s INTEGER)",
29+
TABLE_LINKS, KEY_ID, KEY_URL, KEY_TIMES_SEEN);
30+
Log.d("DEBUG", CREATE_ITEMS_TABLE);
31+
db.execSQL(CREATE_ITEMS_TABLE);
32+
}
33+
34+
@Override
35+
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
36+
if (newVersion == 1) {
37+
db.execSQL("DROP TABLE IF EXISTS " + TABLE_LINKS);
38+
onCreate(db);
39+
}
40+
}
41+
42+
public ImageLinkCount getRecord(String url) {
43+
44+
SQLiteDatabase db = this.getReadableDatabase();
45+
46+
Cursor cursor = db.query(TABLE_LINKS,
47+
new String[] { KEY_ID, KEY_URL, KEY_TIMES_SEEN}, // SELECT
48+
String.format("%s= ?", KEY_URL), new String[] {url},
49+
null, null, null, null);
50+
51+
if (cursor != null) {
52+
if (!cursor.moveToFirst()) {
53+
return null;
54+
}
55+
ImageLinkCount item = new ImageLinkCount(cursor.getString(1), cursor.getInt(2));
56+
item.setId(cursor.getInt(0));
57+
return item;
58+
}
59+
return null;
60+
}
61+
62+
public ImageLinkCount createRecord(String url) {
63+
64+
SQLiteDatabase db = this.getWritableDatabase();
65+
66+
ContentValues values = new ContentValues();
67+
values.put(KEY_URL, url);
68+
values.put(KEY_TIMES_SEEN, 0);
69+
70+
long row = db.insertOrThrow(TABLE_LINKS, null, values);
71+
db.close();
72+
73+
if (row != -1) {
74+
ImageLinkCount item = new ImageLinkCount(url, 0);
75+
item.setId(row);
76+
return item;
77+
}
78+
return null;
79+
}
80+
81+
public int updateItem(ImageLinkCount item) {
82+
SQLiteDatabase db = this.getWritableDatabase();
83+
84+
ContentValues values = new ContentValues();
85+
values.put(KEY_URL, item.getURL());
86+
values.put(KEY_TIMES_SEEN, item.getTimesSeen());
87+
88+
int result = db.update(TABLE_LINKS, values, KEY_ID + " = ?",
89+
new String[] { String.valueOf(item.getId()) });
90+
91+
db.close();
92+
return result;
93+
}
94+
95+
public ImageLinkCount getOrCreate(String url) {
96+
SQLiteDatabase db = this.getWritableDatabase();
97+
98+
ImageLinkCount imageLinkCount = getRecord(url);
99+
100+
if (imageLinkCount == null) {
101+
return createRecord(url);
102+
} else {
103+
}
104+
db.close();
105+
return imageLinkCount;
106+
}
107+
108+
}
109+
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package codepath.com.myfun;
2+
3+
import android.graphics.Bitmap;
4+
import android.graphics.BitmapFactory;
5+
import android.os.AsyncTask;
6+
import android.widget.ImageView;
7+
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.net.HttpURLConnection;
11+
import java.net.URL;
12+
13+
public class Kittens {
14+
15+
public static Kittens kittens;
16+
17+
public static void load(String url, ImageView imageView) {
18+
if (kittens == null) {
19+
kittens = new Kittens();
20+
}
21+
kittens.loadPrivate(url, imageView);
22+
ImageLinksCountDatabase imageLinkCountDatabase = new ImageLinksCountDatabase(imageView.getContext());
23+
ImageLinkCount imageLinkCount = imageLinkCountDatabase.getOrCreate(url);
24+
imageLinkCount.incrementTimesSeen();
25+
imageLinkCountDatabase.updateItem(imageLinkCount);
26+
}
27+
28+
protected void loadPrivate(String url, ImageView imageView) {
29+
new ImageLoader(imageView).execute(url);
30+
}
31+
32+
public class ImageLoader extends AsyncTask<String, Void, Bitmap> {
33+
34+
// make a URL connection
35+
// open a stream
36+
// convert the bytes
37+
38+
private ImageView imageView;
39+
40+
public ImageLoader(ImageView imageView) {
41+
this.imageView = imageView;
42+
}
43+
@Override
44+
protected Bitmap doInBackground(String... params) {
45+
try {
46+
URL url = new URL(params[0]);
47+
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
48+
connection.connect();
49+
InputStream in = connection.getInputStream();
50+
Bitmap bitmap = BitmapFactory.decodeStream(in);
51+
in.close();
52+
return bitmap;
53+
} catch (IOException e) {
54+
}
55+
return null;
56+
}
57+
58+
@Override
59+
protected void onPostExecute(Bitmap bitmap) {
60+
super.onPostExecute(bitmap);
61+
imageView.setImageBitmap(bitmap);
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)