Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boloutare Doubeni - Email App #5

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
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
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
68 changes: 0 additions & 68 deletions README.md

This file was deleted.

1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
37 changes: 37 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
applicationId "com.boloutaredoubeni.emailapp"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dataBinding {
enabled = true
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.0'
compile 'com.android.support:design:23.2.0'
compile 'com.google.android.gms:play-services-identity:8.4.0'
compile('com.google.api-client:google-api-client-android:1.20.0') {
exclude group: 'org.apache.httpcomponents'
}
compile('com.google.apis:google-api-services-gmail:v1-rev29-1.20.0') {
exclude group: 'org.apache.httpcomponents'
}
}
17 changes: 17 additions & 0 deletions 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 /usr/local/opt/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 *;
#}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.boloutaredoubeni.emailapp;

import android.app.Application;
import android.test.ApplicationTestCase;

/**
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing
* Fundamentals</a>
*/
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() { super(Application.class); }
}
48 changes: 48 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.boloutaredoubeni.emailapp"
xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS"/>
<uses-permission android:name="android.permission.USE_CREDENTIALS"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".activities.MainActivity"
android:label="@string/app_name"
android:screenOrientation="user"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

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

<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>

<activity
android:name=".activities.EmailDetailActivity"
android:parentActivityName=".activities.MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activities.MainActivity"/>
</activity>
<activity android:name=".activities.ComposeEmailActivity"
android:parentActivityName=".activities.MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activities.MainActivity"/>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.boloutaredoubeni.emailapp;

import android.app.Application;

import com.google.api.services.gmail.GmailScopes;

/**
* Copyright 2016 Boloutare Doubeni
*/
public class EmailApplication extends Application {
public static final String PREF_ACCOUNT_NAME = "accountName";
private static final String[] SCOPES = {GmailScopes.GMAIL_COMPOSE, GmailScopes.GMAIL_READONLY};

public static String[] getScopes() { return SCOPES; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package com.boloutaredoubeni.emailapp.activities;

import android.content.Context;
import android.content.SharedPreferences;
import android.databinding.DataBindingUtil;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.boloutaredoubeni.emailapp.EmailApplication;
import com.boloutaredoubeni.emailapp.R;
import com.boloutaredoubeni.emailapp.databinding.ActivityComposeEmailBinding;
import com.boloutaredoubeni.emailapp.models.Email;
import com.boloutaredoubeni.emailapp.viewmodels.EmailViewModel;
import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.ExponentialBackOff;
import com.google.api.services.gmail.model.Message;

import java.io.IOException;
import java.util.Arrays;

/**
* An activity for composing emails
* This activity should navigable from the main activity and from a reply action
*/
public class ComposeEmailActivity extends AppCompatActivity {

private GoogleAccountCredential mCredential;
private EmailViewModel mViewModel;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences settings = getPreferences(Context.MODE_PRIVATE);
mCredential =
GoogleAccountCredential.usingOAuth2(
getApplicationContext(),
Arrays.asList(EmailApplication.getScopes()))
.setBackOff(new ExponentialBackOff())
.setSelectedAccountName(
settings.getString(EmailApplication.PREF_ACCOUNT_NAME, null));

ActivityComposeEmailBinding binding =
DataBindingUtil.setContentView(this, R.layout.activity_compose_email);
mViewModel = new EmailViewModel();
binding.setVm(mViewModel);
binding.emailEdit.addTextChangedListener(mViewModel.toFieldWatcher);
binding.subjectEdit.addTextChangedListener(mViewModel.subjectWatcher);
binding.bodyEdit.addTextChangedListener(mViewModel.bodyWatcher);
}

public void onSendEmail(View v) {
Email email = mViewModel.emitEmail();
if (email.isValid()) {
new SendEmailTask(mCredential).execute(email);
return;
}
Toast.makeText(this, "Something ain't right", Toast.LENGTH_SHORT).show();
}

/**
* Send a user email to the correct recipient, for now It should only support
* starting a new email thread
*/
public class SendEmailTask extends AsyncTask<Email, Void, Void> {
private com.google.api.services.gmail.Gmail mService = null;
private Exception mLastError = null;

public SendEmailTask(GoogleAccountCredential credential) {
HttpTransport transport = AndroidHttp.newCompatibleTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
mService = new com.google.api.services.gmail.Gmail.Builder(transport,
jsonFactory,
credential)
.setApplicationName("Email App")
.build();
}

@Override
protected Void doInBackground(Email... params) {
try {
sendEmail(params[0]);
} catch (IOException e) {
mLastError = e;
cancel(true);
mLastError.printStackTrace();
} finally {
return null;
}
}

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Toast.makeText(ComposeEmailActivity.this, "Email sent!!!",
Toast.LENGTH_SHORT)
.show();
}

@Override
protected void onCancelled() {
// TODO: implement me
}

private void sendEmail(Email email) throws IOException {
String user = "me";
Message message = Email.createMessageFrom(email);
message = mService.users().messages().send(user, message).execute();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.boloutaredoubeni.emailapp.activities;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.boloutaredoubeni.emailapp.fragments.EmailDetailFragment;

public class EmailDetailActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

if (getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_LANDSCAPE) {
finish();
return;
}

if (savedInstanceState == null) {
EmailDetailFragment fragment = new EmailDetailFragment();
fragment.setArguments(getIntent().getExtras());
getSupportFragmentManager()
.beginTransaction()
.add(android.R.id.content, fragment)
.commit();
}
}
}
Loading