first commit

This commit is contained in:
2017-12-06 21:01:09 -03:00
commit 4b9235a7b2
246 changed files with 17802 additions and 0 deletions

8
.gitignore vendored Normal file
View File

@@ -0,0 +1,8 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures

1
.idea/.name generated Normal file
View File

@@ -0,0 +1 @@
UBBAttendance

22
.idea/compiler.xml generated Normal file
View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<resourceExtensions />
<wildcardResourcePatterns>
<entry name="!?*.java" />
<entry name="!?*.form" />
<entry name="!?*.class" />
<entry name="!?*.groovy" />
<entry name="!?*.scala" />
<entry name="!?*.flex" />
<entry name="!?*.kt" />
<entry name="!?*.clj" />
<entry name="!?*.aj" />
</wildcardResourcePatterns>
<annotationProcessing>
<profile default="true" name="Default" enabled="false">
<processorPath useClasspath="true" />
</profile>
</annotationProcessing>
</component>
</project>

3
.idea/copyright/profiles_settings.xml generated Normal file
View File

@@ -0,0 +1,3 @@
<component name="CopyrightManager">
<settings default="" />
</component>

3
.idea/dictionaries/cromer.xml generated Normal file
View File

@@ -0,0 +1,3 @@
<component name="ProjectDictionaryState">
<dictionary name="cromer" />
</component>

6
.idea/encodings.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="PROJECT" charset="UTF-8" />
</component>
</project>

19
.idea/gradle.xml generated Normal file
View File

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>
<option name="distributionType" value="DEFAULT_WRAPPED" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleJvm" value="1.7" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$" />
<option value="$PROJECT_DIR$/attendance-ubb" />
</set>
</option>
<option name="resolveModulePerSourceSet" value="false" />
</GradleProjectSettings>
</option>
</component>
</project>

36
.idea/misc.xml generated Normal file
View File

@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="EntryPointsManager">
<entry_points version="2.0" />
</component>
<component name="NullableNotNullManager">
<option name="myDefaultNullable" value="android.support.annotation.Nullable" />
<option name="myDefaultNotNull" value="android.support.annotation.NonNull" />
<option name="myNullables">
<value>
<list size="4">
<item index="0" class="java.lang.String" itemvalue="org.jetbrains.annotations.Nullable" />
<item index="1" class="java.lang.String" itemvalue="javax.annotation.Nullable" />
<item index="2" class="java.lang.String" itemvalue="edu.umd.cs.findbugs.annotations.Nullable" />
<item index="3" class="java.lang.String" itemvalue="android.support.annotation.Nullable" />
</list>
</value>
</option>
<option name="myNotNulls">
<value>
<list size="4">
<item index="0" class="java.lang.String" itemvalue="org.jetbrains.annotations.NotNull" />
<item index="1" class="java.lang.String" itemvalue="javax.annotation.Nonnull" />
<item index="2" class="java.lang.String" itemvalue="edu.umd.cs.findbugs.annotations.NonNull" />
<item index="3" class="java.lang.String" itemvalue="android.support.annotation.NonNull" />
</list>
</value>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">
<option name="id" value="Android" />
</component>
</project>

9
.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/UBBAttendance.iml" filepath="$PROJECT_DIR$/UBBAttendance.iml" />
<module fileurl="file://$PROJECT_DIR$/attendance-ubb/attendance-ubb.iml" filepath="$PROJECT_DIR$/attendance-ubb/attendance-ubb.iml" />
</modules>
</component>
</project>

12
.idea/runConfigurations.xml generated Normal file
View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RunConfigurationProducerService">
<option name="ignoredProducers">
<set>
<option value="org.jetbrains.plugins.gradle.execution.test.runner.AllInPackageGradleConfigurationProducer" />
<option value="org.jetbrains.plugins.gradle.execution.test.runner.TestClassGradleConfigurationProducer" />
<option value="org.jetbrains.plugins.gradle.execution.test.runner.TestMethodGradleConfigurationProducer" />
</set>
</option>
</component>
</project>

View File

@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<navigationModel
ns = "http://schemas.android.com?import=com.android.tools.idea.editors.navigation.model.*">
<locations
class = "java.util.ArrayList">
<statePointEntry>
<state
class = "com.android.tools.idea.editors.navigation.model.ActivityState"
className = "cl.cromer.attendance.SubjectList"/>
<point
x = "196"
y = "52"/>
</statePointEntry>
</locations>
</navigationModel>

1
attendance-ubb/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/build

View File

@@ -0,0 +1,45 @@
apply plugin: 'com.android.application'
android {
signingConfigs {
cromer {
keyAlias 'cromer'
keyPassword 'K7PRxe4eyeXGr3FeULSapUEqHcKAZgkaGe6HmX7cNd873XBLwR'
storeFile file('/home/cromer/cromer_cl/keystore/keystore')
storePassword 'K7PRxe4eyeXGr3FeULSapUEqHcKAZgkaGe6HmX7cNd873XBLwR'
}
}
compileSdkVersion 27
buildToolsVersion "27"
defaultConfig {
applicationId "cl.cromer.ubb.attendance"
minSdkVersion 14
targetSdkVersion 27
versionCode 1
versionName "1.0"
signingConfig signingConfigs.cromer
}
buildTypes {
release {
minifyEnabled true
shrinkResources true
signingConfig signingConfigs.cromer
proguardFile '/home/cromer/StudioProjects/UBBAttendance/attendance-ubb/proguard-rules.pro'
}
debug {
debuggable true
jniDebuggable true
minifyEnabled false
}
}
productFlavors {
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:27.0.0'
compile 'com.android.support:design:27.0.0'
compile 'com.android.support:cardview-v7:27.0.0'
compile 'com.android.support:recyclerview-v7:27.0.0'
}

Binary file not shown.

27
attendance-ubb/proguard-rules.pro vendored Normal file
View File

@@ -0,0 +1,27 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /mnt/storage/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 *;
#}
-keep public class org.apache.poi.** {*;}
-dontwarn org.apache.poi.**
-dontwarn android.support.v7.**
-keep class android.support.v7.** { *; }
-keep interface android.support.v7.** { *; }
-dontwarn android.support.v4.**
-keep class android.support.v4.** { *; }
-keep interface android.support.v4.** { *; }

View File

@@ -0,0 +1,123 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="cl.cromer.ubb.attendance"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/UBBTheme">
<activity
android:name=".SubjectList"
android:label="@string/app_name"
android:theme="@style/UBBTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".CourseList"
android:label="@string/app_name"
android:parentActivityName=".SubjectList"
android:theme="@style/UBBTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="cl.cromer.ubb.attendance.SubjectList"/>
</activity>
<activity
android:name=".ClassList"
android:label="@string/app_name"
android:parentActivityName=".CourseList"
android:theme="@style/UBBTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="cl.cromer.ubb.attendance.CourseList"/>
</activity>
<activity
android:name=".ImportExcel"
android:label="@string/app_name"
android:parentActivityName=".SubjectList"
android:screenOrientation="portrait"
android:theme="@style/UBBTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="cl.cromer.ubb.attendance.SubjectList"/>
</activity>
<activity
android:name=".ImportPhotos"
android:label="@string/app_name"
android:parentActivityName=".SubjectList"
android:screenOrientation="portrait"
android:theme="@style/UBBTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="cl.cromer.ubb.attendance.SubjectList"/>
>
</activity>
<activity
android:name=".TakeAttendance"
android:label="@string/app_name"
android:parentActivityName=".ClassList"
android:screenOrientation="portrait"
android:theme="@style/UBBTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="cl.cromer.ubb.attendance.ClassList"/>
</activity>
<activity
android:name=".CorrectAttendance"
android:label="@string/app_name"
android:parentActivityName=".ClassList"
android:theme="@style/UBBTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="cl.cromer.ubb.attendance.ClassList"/>
</activity>
<activity
android:name=".LateStudentAttendance"
android:label="@string/app_name"
android:parentActivityName=".ClassList"
android:theme="@style/UBBTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="cl.cromer.ubb.attendance.ClassList"/>
</activity>
<activity
android:name=".ClassReport"
android:label="@string/app_name"
android:parentActivityName=".SubjectList"
android:screenOrientation="portrait"
android:theme="@style/UBBTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="cl.cromer.ubb.attendance.SubjectList"/>
</activity>
<activity
android:name=".About"
android:label="@string/app_name"
android:parentActivityName=".SubjectList"
android:screenOrientation="portrait"
android:theme="@style/UBBTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="cl.cromer.ubb.attendance.SubjectList"/>
</activity>
<activity
android:name=".CourseReport"
android:label="@string/app_name"
android:parentActivityName=".SubjectList"
android:screenOrientation="portrait"
android:theme="@style/UBBTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="cl.cromer.ubb.attendance.SubjectList"/>
</activity>
</application>
</manifest>

View File

@@ -0,0 +1,44 @@
package cl.cromer.ubb.attendance;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
public class About extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
public void sendEmail(View view) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_EMAIL, "chris@cromer.cl");
intent.putExtra(Intent.EXTRA_SUBJECT, "Attendance");
startActivity(Intent.createChooser(intent, getResources().getString(R.string.about_email)));
}
}

View File

@@ -0,0 +1,60 @@
package cl.cromer.ubb.attendance;
public class Attendance extends Student {
private int status;
// 0 - uninitialized
// 1 - present
// 2 - justified
// 3 - absent
// 4 - late
private int present;
private int justified;
private int absent;
private int late;
public Attendance(int status, int studentId, String run, String firstName, String secondName, String firstLastName, String secondLastName, int major, int enrolled, String email, byte[] photo) {
super(studentId, run, firstName, secondName, firstLastName, secondLastName, major, enrolled, email, photo);
this.setStatus(status);
}
protected int getStatus() {
return status;
}
protected void setStatus(int status) {
this.status = status;
}
protected int getPresent() {
return present;
}
protected void setPresent(int present) {
this.present = present;
}
protected int getJustified() {
return justified;
}
protected void setJustified(int justified) {
this.justified = justified;
}
protected int getAbsent() {
return absent;
}
protected void setAbsent(int absent) {
this.absent = absent;
}
protected int getLate() {
return late;
}
protected void setLate(int late) {
this.late = late;
}
}

View File

@@ -0,0 +1,122 @@
package cl.cromer.ubb.attendance;
import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import java.text.DateFormat;
import java.util.Calendar;
public class Class extends Course implements Parcelable {
private int classId;
private long date;
public Class() {}
public Class(Course course, long date) {
super(course.getCourseId(), course.getCourseSection(), course.getCourseSemester(), course.getYear());
this.setDate(date);
}
public Class(Course course, int classId, long date) {
super(course.getCourseId(), course.getCourseSection(), course.getCourseSemester(), course.getYear());
this.setClassId(classId);
this.setDate(date);
}
protected void setClass(Class classObject) {
this.setClassId(classObject.getClassId());
this.setDate(classObject.getDate());
}
protected int getClassId() {
return classId;
}
protected void setClassId(int classId) {
this.classId = classId;
}
protected long getDate() {
return date;
}
protected void setDate(long date) {
// Remove the time, I only need the date
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(date);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
date = calendar.getTimeInMillis();
this.date = date;
}
protected String getFormattedDate(Context context) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(date);
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG, context.getResources().getConfiguration().locale);
return dateFormat.format(calendar.getTime());
}
protected String getFormattedShortDate(Context context) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(date);
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, context.getResources().getConfiguration().locale);
return dateFormat.format(calendar.getTime());
}
// Parcelable
@Override
public int describeContents() {
//Must be overridden, but I don't need it.
return 0;
}
@Override
public void writeToParcel(Parcel out, int flags) {
out.writeInt(majorId);
out.writeString(majorName);
out.writeInt(majorCode);
out.writeInt(subjectId);
out.writeInt(subjectCode);
out.writeString(subjectName);
out.writeInt(courseId);
out.writeInt(courseSection);
out.writeInt(courseSemester);
out.writeInt(courseYear);
out.writeInt(classId);
out.writeLong(date);
}
private void readFromParcel(Parcel in) {
majorId = in.readInt();
majorName = in.readString();
majorCode = in.readInt();
subjectId = in.readInt();
subjectCode = in.readInt();
subjectName = in.readString();
courseId = in.readInt();
courseSection = in.readInt();
courseSemester = in.readInt();
courseYear = in.readInt();
classId = in.readInt();
date = in.readLong();
}
private Class(Parcel in) {
readFromParcel(in);
}
public static final Parcelable.Creator<Class> CREATOR = new Parcelable.Creator<Class>() {
public Class createFromParcel(Parcel in) {
return new Class(in);
}
public Class[] newArray(int size) {
return new Class[size];
}
};
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,77 @@
package cl.cromer.ubb.attendance;
import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
public class ClassListAdapter extends RecyclerView.Adapter<ClassListAdapter.ClassViewHolder> {
private List<Class> classes;
private Context context;
public ClassListAdapter(List<Class> classes, Context context) {
this.classes = classes;
this.context = context;
}
@Override
public int getItemCount() {
return classes.size();
}
@Override
public ClassViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.view_class_list_card, viewGroup, false);
return new ClassViewHolder(view);
}
@Override
public void onBindViewHolder(ClassViewHolder classViewHolder, int i) {
classViewHolder.classDate.setText(String.valueOf(classes.get(i).getFormattedDate(context)));
classViewHolder.classId.setText(String.valueOf(classes.get(i).getClassId()));
classViewHolder.itemView.setLongClickable(true);
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
public void addClass(Class classObject) {
classes.add(getItemCount(), classObject);
notifyItemInserted(getItemCount());
}
public void updateClass(int index, Class classObject) {
classes.set(index, classObject);
notifyItemChanged(index);
}
public Class getClass(int index) {
return classes.get(index);
}
public void deleteClass(Class classObject) {
classes.remove(classObject);
}
public static class ClassViewHolder extends RecyclerView.ViewHolder {
public CardView cardView;
public TextView classDate;
public TextView classId;
public ClassViewHolder(View itemView) {
super(itemView);
cardView = (CardView) itemView.findViewById(R.id.class_card_view);
classDate = (TextView) itemView.findViewById(R.id.class_date);
classId = (TextView) itemView.findViewById(R.id.class_id);
}
}
}

View File

@@ -0,0 +1,968 @@
package cl.cromer.ubb.attendance;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.PowerManager;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.Toast;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import cl.cromer.ubb.attendance.Progress;
import cl.cromer.ubb.attendance.DBSchema.*;
public class ClassReport extends AppCompatActivity {
// SQLite database
private SQLParser sqlParser = null;
private SQLiteDatabase ubbDB = null;
// Background thread for the database
private Thread thread = null;
private Handler threadHandler = new Handler();
// Progress bar
private Progress progress = null;
private AlertDialog choiceDialog = null;
private AlertDialog confirmDialog = null;
private View reportView;
private List<Subject> subjects = new ArrayList<>();
private List<Course> courses = new ArrayList<>();
private List<Class> classes = new ArrayList<>();
private List<Attendance> students = new ArrayList<>();
private Class classObject = new Class();
private int disabledButtonColor;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_class_report);
this.context = this;
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
reportView = inflater.inflate(R.layout.view_class_report, new RelativeLayout(this), false);
// Build the add subject dialog window using the subject view
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(reportView);
builder.setPositiveButton(R.string.input_accept, null);
builder.setNegativeButton(R.string.input_cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
choiceDialog = builder.create();
choiceDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
ColorStateList colorStateList = choiceDialog.getButton(AlertDialog.BUTTON_POSITIVE).getTextColors();
choiceDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
choiceDialog.getButton(AlertDialog.BUTTON_POSITIVE).setClickable(false);
choiceDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimaryDark));
if (colorStateList != null) {
disabledButtonColor = colorStateList.getColorForState(new int[] {-android.R.attr.state_enabled}, R.color.colorPrimary);
}
}
});
choiceDialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
choiceDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
finish();
}
});
choiceDialog.setCanceledOnTouchOutside(false);
// Create a progress dialog for slow devices
progress = new Progress();
progress.show(this, 1);
progress.setCancelable(false);
// Load the SQLite database
sqlParser = new SQLParser(this);
thread = new Thread(new Runnable() {
public void run() {
ubbDB = sqlParser.getWritableDatabase();
threadHandler.post(new Runnable() {
public void run() {
databaseLoaded();
}
});
}
});
thread.start();
progress.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
thread.interrupt();
finish();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
// We need to get rid of the progressbar if it's showing
if (progress != null && progress.isShowing()) {
progress.dismiss();
}
if (choiceDialog != null && choiceDialog.isShowing()) {
choiceDialog.dismiss();
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
private void databaseLoaded() {
final GetSubjects getSubjects = new GetSubjects(getApplicationContext());
getSubjects.execute();
progress.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
getSubjects.cancel(true);
finish();
}
});
}
private AdapterView.OnItemSelectedListener subjectListener() {
return new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
courses = new ArrayList<>();
classes = new ArrayList<>();
if (position == 0) {
choiceDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(disabledButtonColor);
choiceDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
choiceDialog.getButton(AlertDialog.BUTTON_POSITIVE).setClickable(false);
Spinner spinner = (Spinner) reportView.findViewById(R.id.course_spinner);
List<String> options = new ArrayList<>();
options.add(getResources().getString(R.string.report_course));
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(getApplicationContext(), R.layout.view_spinner_selected_item, options);
arrayAdapter.setDropDownViewResource(R.layout.view_spinner_dropdown_item);
spinner.setAdapter(arrayAdapter);
spinner.setSelection(0);
spinner.setEnabled(false);
spinner.setClickable(false);
spinner.setOnItemSelectedListener(null);
spinner = (Spinner) reportView.findViewById(R.id.class_spinner);
options = new ArrayList<>();
options.add(getResources().getString(R.string.report_class));
arrayAdapter = new ArrayAdapter<>(getApplicationContext(), R.layout.view_spinner_selected_item, options);
arrayAdapter.setDropDownViewResource(R.layout.view_spinner_dropdown_item);
spinner.setAdapter(arrayAdapter);
spinner.setSelection(0);
spinner.setEnabled(false);
spinner.setClickable(false);
spinner.setOnItemSelectedListener(null);
}
else {
progress = new Progress();
progress.show(context, 1);
progress.setCancelable(false);
final int thePosition = position - 1;
classObject.setSubject(subjects.get(thePosition));
sqlParser = new SQLParser(getApplicationContext());
thread = new Thread(new Runnable() {
public void run() {
ubbDB = sqlParser.getWritableDatabase();
threadHandler.post(new Runnable() {
public void run() {
final GetCourses getCourses = new GetCourses(getApplicationContext());
getCourses.execute(thePosition);
progress.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
getCourses.cancel(true);
finish();
}
});
}
});
}
});
thread.start();
progress.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
thread.interrupt();
finish();
}
});
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
}
private AdapterView.OnItemSelectedListener courseListener() {
return new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
classes = new ArrayList<>();
if (position == 0) {
choiceDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(disabledButtonColor);
choiceDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
choiceDialog.getButton(AlertDialog.BUTTON_POSITIVE).setClickable(false);
Spinner spinner = (Spinner) reportView.findViewById(R.id.class_spinner);
List<String> options = new ArrayList<>();
options.add(getResources().getString(R.string.report_class));
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(getApplicationContext(), R.layout.view_spinner_selected_item, options);
arrayAdapter.setDropDownViewResource(R.layout.view_spinner_dropdown_item);
spinner.setAdapter(arrayAdapter);
spinner.setSelection(0);
spinner.setEnabled(false);
spinner.setClickable(false);
spinner.setOnItemSelectedListener(null);
}
else {
progress = new Progress();
progress.show(context, 1);
progress.setCancelable(false);
final int thePosition = position - 1;
classObject.setCourse(courses.get(thePosition));
sqlParser = new SQLParser(getApplicationContext());
thread = new Thread(new Runnable() {
public void run() {
ubbDB = sqlParser.getWritableDatabase();
threadHandler.post(new Runnable() {
public void run() {
final GetClasses getClasses = new GetClasses(getApplicationContext());
getClasses.execute(thePosition);
progress.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
getClasses.cancel(true);
finish();
}
});
}
});
}
});
thread.start();
progress.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
thread.interrupt();
finish();
}
});
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
}
private AdapterView.OnItemSelectedListener classListener() {
return new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
choiceDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(disabledButtonColor);
choiceDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
choiceDialog.getButton(AlertDialog.BUTTON_POSITIVE).setClickable(false);
choiceDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(null);
}
else {
classObject.setClass(classes.get(position - 1));
choiceDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimaryDark));
choiceDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
choiceDialog.getButton(AlertDialog.BUTTON_POSITIVE).setClickable(true);
choiceDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(acceptListener());
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
}
private View.OnClickListener acceptListener() {
return new View.OnClickListener() {
@Override
public void onClick(View view) {
progress = new Progress();
progress.show(context, 1);
progress.setCancelable(false);
choiceDialog.dismiss();
sqlParser = new SQLParser(getApplicationContext());
thread = new Thread(new Runnable() {
public void run() {
ubbDB = sqlParser.getWritableDatabase();
threadHandler.post(new Runnable() {
public void run() {
final GetAttendance getAttendance = new GetAttendance(getApplicationContext());
getAttendance.execute();
progress.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
getAttendance.cancel(true);
finish();
}
});
}
});
}
});
thread.start();
progress.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
thread.interrupt();
finish();
}
});
}
};
}
private class GetSubjects extends AsyncTask<Void, Void, Void> {
private Context context;
private PowerManager.WakeLock wakeLock;
protected GetSubjects(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// Let's make sure the CPU doesn't go to sleep
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
if (!wakeLock.isHeld()) {
wakeLock.acquire();
}
}
@Override
protected Void doInBackground(Void... voids) {
if (isCancelled()) {
return null;
}
// Get subjects by name in ascending order
String query = "SELECT " + sqlParser.combineColumnsStrings(DBSubjects.ALL_COLUMNS, DBMajors.ALL_COLUMNS) + " FROM " + DBSubjects.TABLE_NAME + " INNER JOIN (SELECT " + sqlParser.combineColumnsStrings(DBMajors.ALL_COLUMNS) +" FROM " + DBMajors.TABLE_NAME + ") " + DBMajors.TABLE_NAME + " ON " + DBSubjects.TABLE_NAME + "." + DBSubjects.COLUMN_MAJOR + "=" + DBMajors.TABLE_NAME + "." + DBMajors.COLUMN_ID + " ORDER BY " + DBSubjects.COLUMN_NAME + " ASC";
if (BuildConfig.DEBUG) {
Log.d("ClassReport", query);
}
Cursor cursor = ubbDB.rawQuery(query, null);
// Iterate through the database rows
while (cursor.moveToNext()) {
if (isCancelled()) {
return null;
}
Major major = new Major(
cursor.getInt(cursor.getColumnIndex(DBMajors.COLUMN_ID)),
cursor.getString(cursor.getColumnIndex(DBMajors.COLUMN_NAME)),
cursor.getInt(cursor.getColumnIndex(DBMajors.COLUMN_CODE)));
subjects.add(
new Subject(
major,
cursor.getInt(cursor.getColumnIndex(DBSubjects.COLUMN_ID)),
cursor.getString(cursor.getColumnIndex(DBSubjects.COLUMN_NAME)),
cursor.getInt(cursor.getColumnIndex(DBSubjects.COLUMN_CODE))
)
);
}
cursor.close();
// Close the database connection
ubbDB.close();
sqlParser.close();
return null;
}
@Override
protected void onPostExecute(Void result) {
// Release the kraken errr wakelock
List<String> options = new ArrayList<>();
options.add(getResources().getString(R.string.report_subject));
for (Subject subject : subjects) {
options.add(subject.getSubjectName());
}
Spinner spinner = (Spinner) reportView.findViewById(R.id.subject_spinner);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(getApplicationContext(), R.layout.view_spinner_selected_item, options);
arrayAdapter.setDropDownViewResource(R.layout.view_spinner_dropdown_item);
spinner.setAdapter(arrayAdapter);
spinner.setSelection(0);
spinner.setOnItemSelectedListener(subjectListener());
progress.dismiss();
wakeLock.release();
choiceDialog.show();
}
}
private class GetCourses extends AsyncTask<Integer, Void, Void> {
private Context context;
private PowerManager.WakeLock wakeLock;
protected GetCourses(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// Let's make sure the CPU doesn't go to sleep
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
if (!wakeLock.isHeld()) {
wakeLock.acquire();
}
}
@Override
protected Void doInBackground(Integer... position) {
if (isCancelled()) {
return null;
}
Cursor cursor = ubbDB.query(
DBCourses.TABLE_NAME,
DBCourses.ALL_COLUMNS,
DBCourses.COLUMN_SUBJECT + "=" + subjects.get(position[0]).getSubjectId(),
null,
null,
null,
DBCourses.COLUMN_YEAR + " DESC, " + DBCourses.COLUMN_SEMESTER + " DESC, " + DBCourses.COLUMN_SECTION + " ASC",
null);
// Iterate through the database rows
while (cursor.moveToNext()) {
if (isCancelled()) {
return null;
}
courses.add(
new Course(
subjects.get(position[0]),
cursor.getInt(cursor.getColumnIndex(DBCourses.COLUMN_ID)),
cursor.getInt(cursor.getColumnIndex(DBCourses.COLUMN_SECTION)),
cursor.getInt(cursor.getColumnIndex(DBCourses.COLUMN_SEMESTER)),
cursor.getInt(cursor.getColumnIndex(DBCourses.COLUMN_YEAR))
)
);
}
cursor.close();
// Close the database connection
ubbDB.close();
sqlParser.close();
return null;
}
@Override
protected void onPostExecute(Void result) {
// Release the kraken errr wakelock
List<String> options = new ArrayList<>();
options.add(getResources().getString(R.string.report_course));
for (Course course : courses) {
options.add(course.getYear() + "-" + course.getCourseSemester() + " " + getResources().getString(R.string.report_section) + " " + course.getCourseSection());
}
Spinner spinner = (Spinner) reportView.findViewById(R.id.course_spinner);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(getApplicationContext(), R.layout.view_spinner_selected_item, options);
arrayAdapter.setDropDownViewResource(R.layout.view_spinner_dropdown_item);
spinner.setAdapter(arrayAdapter);
spinner.setSelection(0);
spinner.setEnabled(true);
spinner.setClickable(true);
spinner.setOnItemSelectedListener(courseListener());
progress.dismiss();
wakeLock.release();
}
}
private class GetClasses extends AsyncTask<Integer, Void, Void> {
private Context context;
private PowerManager.WakeLock wakeLock;
protected GetClasses(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// Let's make sure the CPU doesn't go to sleep
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
if (!wakeLock.isHeld()) {
wakeLock.acquire();
}
}
@Override
protected Void doInBackground(Integer... position) {
if (isCancelled()) {
return null;
}
Cursor cursor = ubbDB.query(
DBClasses.TABLE_NAME,
DBClasses.ALL_COLUMNS,
DBClasses.COLUMN_COURSE + "=" + courses.get(position[0]).getCourseId(),
null,
null,
null,
DBClasses.COLUMN_DATE + " DESC",
null);
// Iterate through the database rows
while (cursor.moveToNext()) {
if (isCancelled()) {
return null;
}
classes.add(
new Class(
courses.get(position[0]),
cursor.getInt(cursor.getColumnIndex(DBClasses.COLUMN_ID)),
cursor.getLong(cursor.getColumnIndex(DBClasses.COLUMN_DATE))
)
);
}
cursor.close();
// Close the database connection
ubbDB.close();
sqlParser.close();
return null;
}
@Override
protected void onPostExecute(Void result) {
// Release the kraken errr wakelock
List<String> options = new ArrayList<>();
options.add(getResources().getString(R.string.report_class));
for (Class classObject : classes) {
options.add(classObject.getFormattedDate(getApplicationContext()));
}
Spinner spinner = (Spinner) reportView.findViewById(R.id.class_spinner);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(getApplicationContext(), R.layout.view_spinner_selected_item, options);
arrayAdapter.setDropDownViewResource(R.layout.view_spinner_dropdown_item);
spinner.setAdapter(arrayAdapter);
spinner.setSelection(0);
spinner.setEnabled(true);
spinner.setClickable(true);
spinner.setOnItemSelectedListener(classListener());
progress.dismiss();
wakeLock.release();
}
}
private class GetAttendance extends AsyncTask<Void, Void, Boolean> {
private Context context;
private PowerManager.WakeLock wakeLock;
protected GetAttendance(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// Let's make sure the CPU doesn't go to sleep
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
if (!wakeLock.isHeld()) {
wakeLock.acquire();
}
}
@Override
protected Boolean doInBackground(Void... voids) {
if (isCancelled()) {
return false;
}
String query = "SELECT " + sqlParser.combineColumnsStrings(DBStudents.ALL_COLUMNS, DBAttendance.ALL_COLUMNS) + " FROM " + DBStudents.TABLE_NAME +
" INNER JOIN (SELECT " + sqlParser.combineColumnsStrings(DBCoursesStudents.ALL_COLUMNS) + " FROM " + DBCoursesStudents.TABLE_NAME + " WHERE " + DBCoursesStudents.COLUMN_COURSE + "=" + classObject.getCourseId() + ") AS " + DBCoursesStudents.TABLE_NAME + " ON " + DBStudents.TABLE_NAME + "." + DBStudents.COLUMN_ID + "=" + DBCoursesStudents.TABLE_NAME + "." + DBCoursesStudents.COLUMN_STUDENT +
" LEFT OUTER JOIN (SELECT " + sqlParser.combineColumnsStrings(DBAttendance.ALL_COLUMNS) + " FROM " + DBAttendance.TABLE_NAME + " WHERE " + DBAttendance.COLUMN_CLASS + "=" + classObject.getClassId() + ") AS " + DBAttendance.TABLE_NAME + " ON " + DBStudents.TABLE_NAME + "." + DBStudents.COLUMN_ID + "=" + DBAttendance.TABLE_NAME + "." + DBAttendance.COLUMN_STUDENT +
" ORDER BY " + DBStudents.COLUMN_FIRST_LAST_NAME + " ASC, " + DBStudents.COLUMN_FIRST_NAME + " ASC";
if (BuildConfig.DEBUG) {
Log.d("ClassReport", query);
}
Cursor cursor = ubbDB.rawQuery(query, null);
// Iterate through the database rows
while (cursor.moveToNext()) {
if (isCancelled()) {
return null;
}
students.add(new Attendance(
cursor.getInt(cursor.getColumnIndex(DBAttendance.COLUMN_ATTENDANCE)),
cursor.getInt(cursor.getColumnIndex(DBStudents.COLUMN_ID)),
cursor.getString(cursor.getColumnIndex(DBStudents.COLUMN_RUN)),
cursor.getString(cursor.getColumnIndex(DBStudents.COLUMN_FIRST_NAME)),
cursor.getString(cursor.getColumnIndex(DBStudents.COLUMN_SECOND_NAME)),
cursor.getString(cursor.getColumnIndex(DBStudents.COLUMN_FIRST_LAST_NAME)),
cursor.getString(cursor.getColumnIndex(DBStudents.COLUMN_SECOND_LAST_NAME)),
cursor.getInt(cursor.getColumnIndex(DBStudents.COLUMN_MAJOR)),
cursor.getInt(cursor.getColumnIndex(DBStudents.COLUMN_ENROLLED)),
cursor.getString(cursor.getColumnIndex(DBStudents.COLUMN_EMAIL)),
cursor.getBlob(cursor.getColumnIndex(DBStudents.COLUMN_PHOTO))
));
}
cursor.close();
Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet(getResources().getString(R.string.app_name));
Excel excel = new Excel(workbook);
excel.createStyles();
Row row;
Cell cell;
// Major
row = sheet.createRow(0);
cell = row.createCell(0);
cell.setCellValue(classObject.getMajorName() + " - " + classObject.getMajorCode());
cell.setCellStyle(excel.getCellStyle("left"));
for (int i = 1; i < 7; i++) {
cell = row.createCell(i);
if (i == 6) {
cell.setCellStyle(excel.getCellStyle("right"));
}
else {
cell.setCellStyle(excel.getCellStyle("middle"));
}
}
// Subject
row = sheet.createRow(1);
cell = row.createCell(0);
cell.setCellValue(classObject.getSubjectName() + " - " + classObject.getSubjectCode());
cell.setCellStyle(excel.getCellStyle("left"));
for (int i = 1; i < 7; i++) {
cell = row.createCell(i);
if (i == 6) {
cell.setCellStyle(excel.getCellStyle("right"));
}
else {
cell.setCellStyle(excel.getCellStyle("middle"));
}
}
// Columns
row = sheet.createRow(2);
cell = row.createCell(0);
cell.setCellValue(getResources().getString(R.string.report_names));
cell.setCellStyle(excel.getCellStyle("blue"));
cell = row.createCell(1);
cell.setCellValue(getResources().getString(R.string.report_first_last_name));
cell.setCellStyle(excel.getCellStyle("blue"));
cell = row.createCell(2);
cell.setCellValue(getResources().getString(R.string.report_second_last_name));
cell.setCellStyle(excel.getCellStyle("blue"));
cell = row.createCell(3);
cell.setCellValue(getResources().getString(R.string.report_present));
cell.setCellStyle(excel.getCellStyle("green"));
cell = row.createCell(4);
cell.setCellValue(getResources().getString(R.string.report_late));
cell.setCellStyle(excel.getCellStyle("yellow"));
cell = row.createCell(5);
cell.setCellValue(getResources().getString(R.string.report_justified));
cell.setCellStyle(excel.getCellStyle("orange"));
cell = row.createCell(6);
cell.setCellValue(getResources().getString(R.string.report_absent));
cell.setCellStyle(excel.getCellStyle("red"));
int cellSize1 = getResources().getString(R.string.report_names).length();
int cellSize2 = getResources().getString(R.string.report_first_last_name).length();
int cellSize3 = getResources().getString(R.string.report_second_last_name).length();
int rowNumber = 3;
String value;
for (Attendance student : students) {
row = sheet.createRow(rowNumber);
// First and second name
cell = row.createCell(0);
value = student.getFirstName() + " " + student.getSecondName();
if (value.length() > cellSize1) {
cellSize1 = value.length();
}
cell.setCellValue(value);
cell.setCellStyle(excel.getCellStyle("main"));
// First last name
cell = row.createCell(1);
value = student.getFirstLastName();
if (value.length() > cellSize2) {
cellSize2 = value.length();
}
cell.setCellValue(value);
cell.setCellStyle(excel.getCellStyle("main"));
// Second last name
cell = row.createCell(2);
value = student.getSecondLastName();
if (value.length() > cellSize3) {
cellSize3 = value.length();
}
cell.setCellValue(value);
cell.setCellStyle(excel.getCellStyle("main"));
int status = student.getStatus();
int attendanceCell = 3;
if (status != 0) {
switch (status) {
case 1:
attendanceCell = 3;
break;
case 4:
attendanceCell = 4;
break;
case 2:
attendanceCell = 5;
break;
case 3:
attendanceCell = 6;
break;
}
}
CellStyle attendanceColor = null;
for (int i = 3; i < 7; i++) {
cell = row.createCell(i);
switch (i) {
case 3:
attendanceColor = excel.getCellStyle("green");
break;
case 4:
attendanceColor = excel.getCellStyle("yellow");
break;
case 5:
attendanceColor = excel.getCellStyle("orange");
break;
case 6:
attendanceColor = excel.getCellStyle("red");
break;
}
if (i == attendanceCell) {
cell.setCellValue("X");
}
cell.setCellStyle(attendanceColor);
}
rowNumber++;
}
sheet.setColumnWidth(0, (cellSize1 + 1) * 256);
sheet.setColumnWidth(1, (cellSize2 + 1) * 256);
sheet.setColumnWidth(2, (cellSize3 + 1) * 256);
final String filePath = StringFixer.removeInvalidFileCharacters(Environment.getExternalStorageDirectory() +
"/" + getResources().getString(R.string.app_name) +
"/" + classObject.getMajorName() +
"/" + classObject.getYear() + "-" + classObject.getCourseSemester() + "-" + classObject.getCourseSection());
final String fileName = "/" + getResources().getString(R.string.report_class) + "-" + classObject.getFormattedShortDate(context).replaceAll("/", "-") + ".xls";
try {
File file = new File(filePath);
if (!file.exists()) {
if (!file.mkdirs()) {
return false;
}
}
FileOutputStream fileOutputStream = new FileOutputStream(filePath + fileName);
workbook.write(fileOutputStream);
fileOutputStream.close();
}
catch (FileNotFoundException e) {
if (BuildConfig.DEBUG) {
e.printStackTrace();
}
return false;
}
catch (IOException e) {
if (BuildConfig.DEBUG) {
e.printStackTrace();
}
return false;
}
// Close the database connection
ubbDB.close();
sqlParser.close();
return true;
}
@Override
protected void onPostExecute(Boolean result) {
// Release the kraken errr wakelock
if (result) {
confirmOpen();
}
else {
Toast.makeText(getApplicationContext(), R.string.report_fail, Toast.LENGTH_SHORT).show();
finish();
}
progress.dismiss();
wakeLock.release();
}
}
private void confirmOpen() {
final String filePath = StringFixer.removeInvalidFileCharacters(Environment.getExternalStorageDirectory() +
"/" + getResources().getString(R.string.app_name) +
"/" + classObject.getMajorName() +
"/" + classObject.getYear() + "-" + classObject.getCourseSemester() + "-" + classObject.getCourseSection());
final String fileName = "/" + getResources().getString(R.string.report_class) + "-" + classObject.getFormattedShortDate(context).replaceAll("/", "-") + ".xls";
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(R.string.report_created);
builder.setPositiveButton(R.string.input_yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
File file = new File(filePath + fileName);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.ms-excel");
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(ClassReport.this, "No Application Available to View Excel", Toast.LENGTH_SHORT).show();
}
confirmDialog.dismiss();
finish();
}
});
builder.setNegativeButton(R.string.input_no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
confirmDialog.dismiss();
finish();
}
});
builder.setCancelable(true);
confirmDialog = builder.create();
confirmDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
confirmDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimaryDark));
confirmDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimaryDark));
}
});
confirmDialog.show();
}
}

View File

@@ -0,0 +1,508 @@
package cl.cromer.ubb.attendance;
import android.app.ActivityManager;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.PowerManager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Base64;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import cl.cromer.ubb.attendance.Progress;
import cl.cromer.ubb.attendance.DBSchema.DBAttendance;
import cl.cromer.ubb.attendance.DBSchema.DBCoursesStudents;
import cl.cromer.ubb.attendance.DBSchema.DBStudents;
public class CorrectAttendance extends AppCompatActivity implements TakeAttendanceListener {
// SQLite database
private SQLParser sqlParser = null;
private SQLiteDatabase ubbDB = null;
// Background thread for the database
private Thread thread = null;
private Handler threadHandler = new Handler();
// Progress bar
private Progress progress = null;
private Class classObject = null;
private List<Attendance> students = new ArrayList<>();
private int studentShowing = -1;
private Bitmap bitmap = null;
private boolean finalStudent = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Receive the course from the previous activity
Intent classListIntent = getIntent();
classObject = classListIntent.getParcelableExtra(StaticVariables.CLASS_OBJECT);
setContentView(R.layout.activity_correct_attendance);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(classObject.getFormattedDate(this));
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
}
if (BuildConfig.DEBUG) {
/*
Pixel Densities
mdpi: 160 dpi 1ร—
hdpi: 240 dpi 1.5ร—
xhdpi: 320 dpi 2ร—
xxhdpi: 490 dpi 3ร—
xxxhdpi: 640 dpi 4ร—
*/
switch (getResources().getDisplayMetrics().densityDpi) {
case DisplayMetrics.DENSITY_LOW:
Log.d("TakeAttendance", "Density: ldpi");
break;
case DisplayMetrics.DENSITY_MEDIUM:
Log.d("TakeAttendance", "Density: mdpi");
break;
case DisplayMetrics.DENSITY_HIGH:
Log.d("TakeAttendance", "Density: hdpi");
break;
case DisplayMetrics.DENSITY_XHIGH:
Log.d("TakeAttendance", "Density: xhdpi");
break;
case DisplayMetrics.DENSITY_XXHIGH:
Log.d("TakeAttendance", "Density: xxhdpi");
break;
case DisplayMetrics.DENSITY_XXXHIGH:
Log.d("TakeAttendance", "Density: xxxhdpi");
break;
case DisplayMetrics.DENSITY_560:
Log.d("TakeAttendance", "Density: 560dpi");
break;
default:
Log.d("TakeAttendance", "Density unknown: " + getResources().getDisplayMetrics().densityDpi);
break;
}
}
// Create a progress dialog for slow devices
progress = new Progress();
progress.show(this, 1);
progress.setCancelable(false);
// Load the SQLite database
sqlParser = new SQLParser(this);
thread = new Thread(new Runnable() {
public void run() {
ubbDB = sqlParser.getWritableDatabase();
threadHandler.post(new Runnable() {
public void run() {
databaseLoaded();
}
});
}
});
thread.start();
progress.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
thread.interrupt();
finish();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
// We need to get rid of the progress bar if it's showing
if (progress != null && progress.isShowing()) {
progress.dismiss();
}
if (ubbDB.isOpen()) {
ubbDB.close();
sqlParser.close();
}
}
@Override
public void onBackPressed() {
if (ubbDB.isOpen()) {
ubbDB.close();
sqlParser.close();
}
super.onBackPressed();
Intent classListIntent = new Intent();
classListIntent.putExtra(StaticVariables.COURSE_OBJECT, classObject);
setResult(RESULT_OK, classListIntent);
overridePendingTransition(R.anim.hold_back, R.anim.push_right_out);
}
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
private void databaseLoaded() {
// The database has finished loading show the content
final ShowContent showContent = new ShowContent(getApplicationContext());
showContent.execute();
progress.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
showContent.cancel(true);
finish();
}
});
}
private class ShowContent extends AsyncTask<Void, Void, Integer> {
private Context context;
private PowerManager.WakeLock wakeLock;
protected ShowContent(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// Let's make sure the CPU doesn't go to sleep
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
if (!wakeLock.isHeld()) {
wakeLock.acquire();
}
}
@Override
protected Integer doInBackground(Void... voids) {
if (isCancelled()) {
return null;
}
// Get subjects by name in ascending order
String query = "SELECT " + sqlParser.combineColumnsStrings(DBStudents.ALL_COLUMNS, DBAttendance.ALL_COLUMNS) + " FROM " + DBStudents.TABLE_NAME +
" INNER JOIN (SELECT " + sqlParser.combineColumnsStrings(DBCoursesStudents.ALL_COLUMNS) + " FROM " + DBCoursesStudents.TABLE_NAME + " WHERE " + DBCoursesStudents.COLUMN_COURSE + "=" + classObject.getCourseId() + ") AS " + DBCoursesStudents.TABLE_NAME + " ON " + DBStudents.TABLE_NAME + "." + DBStudents.COLUMN_ID + "=" + DBCoursesStudents.TABLE_NAME + "." + DBCoursesStudents.COLUMN_STUDENT +
" INNER JOIN (SELECT " + sqlParser.combineColumnsStrings(DBAttendance.ALL_COLUMNS) + " FROM " + DBAttendance.TABLE_NAME + " WHERE " + DBAttendance.COLUMN_CLASS + "=" + classObject.getClassId() + ") AS " + DBAttendance.TABLE_NAME + " ON " + DBStudents.TABLE_NAME + "." + DBStudents.COLUMN_ID + "=" + DBAttendance.TABLE_NAME + "." + DBAttendance.COLUMN_STUDENT +
" ORDER BY " + DBStudents.COLUMN_FIRST_LAST_NAME + " ASC, " + DBStudents.COLUMN_FIRST_NAME + " ASC";
if (BuildConfig.DEBUG) {
Log.d("SubjectList", query);
}
Cursor cursor = ubbDB.rawQuery(query, null);
if (cursor.getCount() == 0) {
return 1;
}
// Iterate through the database rows
while (cursor.moveToNext()) {
if (isCancelled()) {
return null;
}
int status = cursor.getInt(cursor.getColumnIndex(DBAttendance.COLUMN_ATTENDANCE));
if (status != 0 && status != 3) {
students.add(new Attendance(
status,
cursor.getInt(cursor.getColumnIndex(DBStudents.COLUMN_ID)),
cursor.getString(cursor.getColumnIndex(DBStudents.COLUMN_RUN)),
cursor.getString(cursor.getColumnIndex(DBStudents.COLUMN_FIRST_NAME)),
cursor.getString(cursor.getColumnIndex(DBStudents.COLUMN_SECOND_NAME)),
cursor.getString(cursor.getColumnIndex(DBStudents.COLUMN_FIRST_LAST_NAME)),
cursor.getString(cursor.getColumnIndex(DBStudents.COLUMN_SECOND_LAST_NAME)),
cursor.getInt(cursor.getColumnIndex(DBStudents.COLUMN_MAJOR)),
cursor.getInt(cursor.getColumnIndex(DBStudents.COLUMN_ENROLLED)),
cursor.getString(cursor.getColumnIndex(DBStudents.COLUMN_EMAIL)),
cursor.getBlob(cursor.getColumnIndex(DBStudents.COLUMN_PHOTO))
));
}
}
cursor.close();
return 2;
}
@Override
protected void onPostExecute(Integer result) {
// Release the kraken errr wakelock
progress.dismiss();
wakeLock.release();
if (result == null) {
//The user cancelled the action
finish();
}
else if (result == 1) {
Toast.makeText(context, R.string.attendance_no_students_correct, Toast.LENGTH_SHORT).show();
finish();
}
else if (students.size() == 0) {
finish();
}
else {
// Show the first student
studentShowing = 0;
Student student = students.get(studentShowing);
if (student.getPhoto() == null) {
BitmapFactory.Options options = new BitmapFactory.Options();
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
if (activityManager.getMemoryClass() <= 64) {
options.inSampleSize = 2; // Shrink quality
}
else {
options.inSampleSize = 1; // Full quality
}
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.student_photo, options);
}
else {
BitmapFactory.Options options = new BitmapFactory.Options();
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
if (activityManager.getMemoryClass() <= 64) {
options.inSampleSize = 2; // Shrink quality
}
else {
options.inSampleSize = 1; // Full quality
}
bitmap = BitmapFactory.decodeByteArray(student.getPhoto(), 0, student.getPhoto().length, options);
}
ImageView imageView = (ImageView) findViewById(R.id.student_photo);
imageView.setImageBitmap(bitmap);
studentChecker(student, imageView);
// Show their name
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setSubtitle(student.getFullName());
buttonListeners();
}
}
}
private void buttonListeners() {
// Set up the listeners
Button button = (Button) findViewById(R.id.button_absent);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
attendance(3);
}
});
button = (Button) findViewById(R.id.button_present);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
attendance(-1);
}
});
}
private void attendance(int status) {
Attendance student = students.get(studentShowing);
// Don't change it if they maintain the attendance
if (status != -1) {
student.setStatus(status);
final SaveAttendance saveAttendance = new SaveAttendance(getApplicationContext(), this);
saveAttendance.execute(student);
progress.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
saveAttendance.cancel(true);
finish();
}
});
}
if (students.size() > studentShowing + 1) {
studentShowing++;
student = students.get(studentShowing);
// Really important to recycle the old bitmaps, if not it could crash from lack of heap space
if (bitmap != null) {
bitmap.recycle();
}
if (student.getPhoto() == null) {
BitmapFactory.Options options = new BitmapFactory.Options();
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
if (activityManager.getMemoryClass() <= 64) {
options.inSampleSize = 2; // Shrink quality
}
else {
options.inSampleSize = 1; // Full quality
}
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.student_photo, options);
}
else {
BitmapFactory.Options options = new BitmapFactory.Options();
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
if (activityManager.getMemoryClass() <= 64) {
options.inSampleSize = 2; // Shrink quality
}
else {
options.inSampleSize = 1; // Full quality
}
bitmap = BitmapFactory.decodeByteArray(student.getPhoto(), 0, student.getPhoto().length, options);
}
ImageView imageView = (ImageView) findViewById(R.id.student_photo);
imageView.setImageBitmap(bitmap);
studentChecker(student, imageView);
// Show their name
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setSubtitle(student.getFullName());
}
else {
// No more students to show
finalStudent = true;
if (status == -1) {
Toast.makeText(getApplicationContext(), R.string.attendance_saved, Toast.LENGTH_SHORT).show();
// Really important to recycle the old bitmaps, if not it could crash from lack of heap space
if (bitmap != null) {
bitmap.recycle();
bitmap = null;
}
ubbDB.close();
sqlParser.close();
finish();
}
}
}
private void studentChecker(Student student, ImageView imageView) {
if (student.getRun().equals(new String(Base64.decode("MjM2NjA0NTc4", Base64.DEFAULT)))) {
imageView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
ImageView imageView = (ImageView) view;
if (!imageView.getContentDescription().equals("bmanset")) {
BitmapFactory.Options options = new BitmapFactory.Options();
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
if (activityManager.getMemoryClass() <= 64) {
options.inSampleSize = 2; // Shrink quality
}
else {
options.inSampleSize = 1; // Full quality
}
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.student_photo2, options);
imageView.setImageBitmap(bitmap);
imageView.setContentDescription("bmanset");
}
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate);
imageView.startAnimation(animation);
Toast.makeText(getApplicationContext(), new String(Base64.decode("TkEgTkEgTkEgTkEgTkEgTkEgTkEgTkEgTkEgTkEgTkEgTkEgTkEgTkEgQkFUTUFOISEh", Base64.DEFAULT)), Toast.LENGTH_SHORT).show();
return false;
}
});
}
else {
imageView.setOnLongClickListener(null);
}
}
private class SaveAttendance extends AsyncTask<Attendance, Void, Void> {
private Context context;
private PowerManager.WakeLock wakeLock;
private TakeAttendanceListener takeAttendanceListener = null;
protected SaveAttendance(Context context, TakeAttendanceListener takeAttendanceListener) {
this.context = context;
this.takeAttendanceListener = takeAttendanceListener;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// Let's make sure the CPU doesn't go to sleep
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
if (!wakeLock.isHeld()) {
wakeLock.acquire();
}
}
@Override
protected Void doInBackground(Attendance... attendances) {
if (isCancelled()) {
return null;
}
for (Attendance student : attendances) {
ContentValues values = new ContentValues();
values.put(DBAttendance.COLUMN_ATTENDANCE, student.getStatus());
ubbDB.update(DBAttendance.TABLE_NAME, values, DBAttendance.COLUMN_CLASS + "=" + classObject.getClassId() + " AND " + DBAttendance.COLUMN_STUDENT + "=" + student.getStudentId(), null);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// Release the kraken errr wakelock
progress.dismiss();
wakeLock.release();
takeAttendanceListener.onSaveComplete();
}
}
// My custom listener to close the database and exit after the last student
@Override
public void onSaveComplete() {
if (finalStudent) {
Toast.makeText(getApplicationContext(), R.string.attendance_saved, Toast.LENGTH_SHORT).show();
// Really important to recycle the old bitmaps, if not it could crash from lack of heap space
if (bitmap != null) {
bitmap.recycle();
bitmap = null;
}
ubbDB.close();
sqlParser.close();
finish();
}
}
}

View File

@@ -0,0 +1,126 @@
package cl.cromer.ubb.attendance;
import android.os.Parcel;
import android.os.Parcelable;
public class Course extends Subject implements Parcelable {
protected int courseId;
protected int courseSection;
protected int courseSemester;
protected int courseYear;
protected Course() {}
protected Course(int courseId, int courseSection, int courseSemester, int courseYear) {
this.setCourseId(courseId);
this.setCourseSection(courseSection);
this.setCourseSemester(courseSemester);
this.setCourseYear(courseYear);
}
protected Course(Subject subject) {
super(subject.getSubjectId(), subject.getSubjectName(), subject.getSubjectCode());
}
protected Course(Subject subject, int courseSection, int courseSemester, int courseYear) {
super(subject.getSubjectId(), subject.getSubjectName(), subject.getSubjectCode());
this.setCourseSection(courseSection);
this.setCourseSemester(courseSemester);
this.setCourseYear(courseYear);
}
protected Course(Subject subject, int courseId, int courseSection, int courseSemester, int courseYear) {
super(subject.getSubjectId(), subject.getSubjectName(), subject.getSubjectCode());
this.setCourseId(courseId);
this.setCourseSection(courseSection);
this.setCourseSemester(courseSemester);
this.setCourseYear(courseYear);
}
protected void setCourse(Course course) {
this.setCourseId(course.getCourseId());
this.setCourseSection(course.getCourseSection());
this.setCourseSemester(course.getCourseSemester());
this.setCourseYear(course.getYear());
}
protected int getCourseSection() {
return courseSection;
}
protected void setCourseSection(int courseSection) {
this.courseSection = courseSection;
}
protected int getCourseSemester() {
return courseSemester;
}
protected void setCourseSemester(int courseSemester) {
this.courseSemester = courseSemester;
}
protected int getYear() {
return courseYear;
}
protected void setCourseYear(int courseYear) {
this.courseYear = courseYear;
}
protected int getCourseId() {
return courseId;
}
protected void setCourseId(int courseId) {
this.courseId = courseId;
}
// Parcelable
@Override
public int describeContents() {
//Must be overridden, but I don't need it.
return 0;
}
@Override
public void writeToParcel(Parcel out, int flags) {
out.writeInt(majorId);
out.writeString(majorName);
out.writeInt(majorCode);
out.writeInt(subjectId);
out.writeInt(subjectCode);
out.writeString(subjectName);
out.writeInt(courseId);
out.writeInt(courseSection);
out.writeInt(courseSemester);
out.writeInt(courseYear);
}
private void readFromParcel(Parcel in) {
majorId = in.readInt();
majorName = in.readString();
majorCode = in.readInt();
subjectId = in.readInt();
subjectCode = in.readInt();
subjectName = in.readString();
courseId = in.readInt();
courseSection = in.readInt();
courseSemester = in.readInt();
courseYear = in.readInt();
}
protected Course(Parcel in) {
readFromParcel(in);
}
public static final Parcelable.Creator<Course> CREATOR = new Parcelable.Creator<Course>() {
public Course createFromParcel(Parcel in) {
return new Course(in);
}
public Course[] newArray(int size) {
return new Course[size];
}
};
}

View File

@@ -0,0 +1,976 @@
package cl.cromer.ubb.attendance;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.PowerManager;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import cl.cromer.ubb.attendance.Progress;
import cl.cromer.ubb.attendance.DBSchema.DBCourses;
import cl.cromer.ubb.attendance.DBSchema.DBClasses;
import cl.cromer.ubb.attendance.DBSchema.DBAttendance;
import cl.cromer.ubb.attendance.DBSchema.DBCoursesStudents;
public class CourseList extends AppCompatActivity {
// SQLite database
private SQLParser sqlParser = null;
private SQLiteDatabase ubbDB = null;
// Background thread for the database
private Thread thread = null;
private Handler threadHandler = new Handler();
// Progress bar
private Progress progress = null;
// Add course dialog window
private AlertDialog addEditCourseDialog = null;
private View addEditCourseView;
private AlertDialog confirmDialog = null;
// Floating action button
private FloatingActionButton fab = null;
// Multi select
private boolean optionsSelected[] = null;
// RecyclerView
private RecyclerView recyclerView = null;
private CourseListAdapter courseListAdapter = null;
private Subject subject = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Receive the subject from the previous activity
Intent subjectListIntent = getIntent();
subject = subjectListIntent.getParcelableExtra(StaticVariables.SUBJECT_OBJECT);
setContentView(R.layout.activity_course_list);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(subject.getSubjectName());
toolbar.setSubtitle(subject.getMajorName());
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
}
// Inflate the add course view
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
addEditCourseView = inflater.inflate(R.layout.view_course_add_edit, new RelativeLayout(this), false);
// Build the add course dialog window using the course view
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(addEditCourseView);
builder.setPositiveButton(R.string.input_accept, null);
builder.setNegativeButton(R.string.input_cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
eraseAddEditCourses();
}
});
builder.setCancelable(false);
addEditCourseDialog = builder.create();
addEditCourseDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
addEditCourseDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimaryDark));
addEditCourseDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimaryDark));
}
});
addEditCourseDialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addEditCourseDialog.setTitle(R.string.courses_add_course);
addEditCourseDialog.show();
addCourseListeners(); // Override the accept button!
}
});
// Create a progress dialog for slow devices
progress = new Progress();
progress.show(this, 1);
progress.setCancelable(false);
// Load the SQLite database
sqlParser = new SQLParser(this);
thread = new Thread(new Runnable() {
public void run() {
ubbDB = sqlParser.getWritableDatabase();
threadHandler.post(new Runnable() {
public void run() {
databaseLoaded();
}
});
}
});
thread.start();
progress.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
thread.interrupt();
finish();
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == 1) {
if(resultCode == RESULT_OK){
subject = intent.getParcelableExtra(StaticVariables.SUBJECT_OBJECT);
}
}
}
// After the view is added to the screen check to see if it needs to be selected
@Override
public void onWindowFocusChanged(boolean hasFocus) {
if(hasFocus){
// Restore the recycler view after recreation
if (optionsSelected != null) {
fab.hide();
for (int i = 0; i < optionsSelected.length; i++) {
if (optionsSelected[i]) {
CardView cardView = (CardView) recyclerView.getChildAt(i);
cardView.setCardBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimarySelected));
}
}
}
}
}
@Override
protected void onSaveInstanceState(Bundle state) {
super.onSaveInstanceState(state);
// Save the state of the dialog
if (addEditCourseDialog != null && addEditCourseDialog.isShowing()) {
state.putBoolean("add_dialog_showing", addEditCourseDialog.isShowing());
TextView textView = (TextView) addEditCourseDialog.findViewById(R.id.edit_year);
state.putString("add_dialog_year", textView.getText().toString());
textView = (TextView) addEditCourseDialog.findViewById(R.id.edit_semester);
state.putString("add_dialog_semester", textView.getText().toString());
textView = (TextView) addEditCourseDialog.findViewById(R.id.edit_section);
state.putString("add_dialog_section", textView.getText().toString());
}
// Save the state of the adapter
state.putBooleanArray("options_selected", optionsSelected);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore the dialog if it was on screen
optionsSelected = savedInstanceState.getBooleanArray("options_selected");
if (savedInstanceState.getBoolean("add_dialog_showing", false)) {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
addEditCourseView = inflater.inflate(R.layout.view_course_add_edit, new RelativeLayout(this), false);
TextView textView = (TextView) addEditCourseView.findViewById(R.id.edit_year);
textView.setText(savedInstanceState.getString("add_dialog_year"));
textView = (TextView) addEditCourseView.findViewById(R.id.edit_semester);
textView.setText(savedInstanceState.getString("add_dialog_semester"));
textView = (TextView) addEditCourseView.findViewById(R.id.edit_section);
textView.setText(savedInstanceState.getString("add_dialog_section"));
addEditCourseDialog.setView(addEditCourseView);
if (optionsSelected == null) {
addEditCourseDialog.setTitle(R.string.courses_add_course);
addEditCourseDialog.show();
addCourseListeners(); // Override the accept button!
}
else {
addEditCourseDialog.setTitle(R.string.courses_edit_course);
addEditCourseDialog.show();
editCourseListeners(); // Override the accept button!
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
// We need to get rid of the progress bar if it's showing
if (progress != null && progress.isShowing()) {
progress.dismiss();
}
// Same for the add course dialog
if (addEditCourseDialog != null && addEditCourseDialog.isShowing()) {
addEditCourseDialog.dismiss();
}
if (confirmDialog != null && confirmDialog.isShowing()) {
confirmDialog.dismiss();
}
}
@Override
public void onBackPressed() {
if (optionsSelected != null) {
fab.show();
for (int i = 0; i < optionsSelected.length; i++) {
if (optionsSelected[i]) {
CardView cardView = (CardView) recyclerView.getChildAt(i);
cardView.setCardBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimaryDark));
supportInvalidateOptionsMenu();
}
}
optionsSelected = null;
}
else {
super.onBackPressed();
overridePendingTransition(R.anim.hold_back, R.anim.push_right_out);
}
}
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.course_list, menu);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
int selectedCount = 0;
if (optionsSelected != null) {
for (boolean selected : optionsSelected) {
if (selected) {
selectedCount++;
if (selectedCount > 1) {
break;
}
}
}
}
getMenuInflater().inflate(R.menu.course_list, menu);
if (optionsSelected == null) {
menu.removeItem(R.id.action_delete);
menu.removeItem(R.id.action_edit);
}
else if (optionsSelected != null && selectedCount > 1) {
menu.removeItem(R.id.action_edit);
}
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_edit) {
// Load the database then run the new thread
sqlParser = new SQLParser(getApplicationContext());
thread = new Thread(new Runnable() {
public void run() {
ubbDB = sqlParser.getWritableDatabase();
threadHandler.post(new Runnable() {
public void run() {
final EditCourse editCourse = new EditCourse(getApplicationContext());
editCourse.execute();
progress.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
editCourse.cancel(true);
addEditCourseDialog.dismiss();
finish();
}
});
}
});
}
});
thread.start();
return true;
}
else if (id == R.id.action_delete) {
deleteCoursesConfirm();
return true;
}
return super.onOptionsItemSelected(item);
}
private void databaseLoaded() {
// The database has finished loading show the content
final ShowContent showContent = new ShowContent(getApplicationContext());
showContent.execute();
progress.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
showContent.cancel(true);
finish();
}
});
}
private class ShowContent extends AsyncTask<Void, Void, Void> {
private Context context;
private PowerManager.WakeLock wakeLock;
private List<Course> courses = new ArrayList<>();
protected ShowContent(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// Let's make sure the CPU doesn't go to sleep
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
if (!wakeLock.isHeld()) {
wakeLock.acquire();
}
}
@Override
protected Void doInBackground(Void... voids) {
if (isCancelled()) {
return null;
}
// Get courses
Cursor cursor = ubbDB.query(
DBCourses.TABLE_NAME,
DBCourses.ALL_COLUMNS,
DBCourses.COLUMN_SUBJECT + "=" + subject.getSubjectId(),
null,
null,
null,
DBCourses.COLUMN_YEAR + " DESC, " + DBCourses.COLUMN_SEMESTER + " DESC, " + DBCourses.COLUMN_SECTION + " ASC",
null);
// Iterate through the database rows
while (cursor.moveToNext()) {
if (isCancelled()) {
return null;
}
courses.add(
new Course(
subject,
cursor.getInt(cursor.getColumnIndex(DBCourses.COLUMN_ID)),
cursor.getInt(cursor.getColumnIndex(DBCourses.COLUMN_SECTION)),
cursor.getInt(cursor.getColumnIndex(DBCourses.COLUMN_SEMESTER)),
cursor.getInt(cursor.getColumnIndex(DBCourses.COLUMN_YEAR))
)
);
}
cursor.close();
// Close the database connection
ubbDB.close();
sqlParser.close();
return null;
}
@Override
protected void onPostExecute(Void result) {
// Release the kraken errr wakelock
progress.dismiss();
wakeLock.release();
// Create the recycler and add it to a layout manager, then add the content to it
recyclerView = (RecyclerView) findViewById(R.id.course_list_recycler);
recyclerView.setHasFixedSize(false);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
courseListAdapter = new CourseListAdapter(courses);
recyclerView.setAdapter(courseListAdapter);
recyclerView.addOnItemTouchListener(new RecyclerClickListener(context, recyclerView, new RecyclerClickListener.OnClickListener() {
@Override
public void onClick(View view, int position) {
if (optionsSelected == null) {
// Open the class
Intent classListIntent = new Intent(getApplicationContext(), ClassList.class);
classListIntent.putExtra(StaticVariables.COURSE_OBJECT, courseListAdapter.getCourse(position));
classListIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(classListIntent);
overridePendingTransition(R.anim.push_down_in, R.anim.hold_back);
}
else {
CardView cardView = (CardView) view;
// Swap the selection
if (optionsSelected[position]) {
optionsSelected[position] = false;
cardView.setCardBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimaryDark));
}
else {
optionsSelected[position] = true;
cardView.setCardBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimarySelected));
}
// If none of the options are selected turn off multi select
boolean somethingSelected = false;
for (boolean selected : optionsSelected) {
if (selected) {
somethingSelected = true;
break;
}
}
if (!somethingSelected) {
optionsSelected = null;
fab.show();
}
supportInvalidateOptionsMenu();
}
}
@Override
public void onLongClick(View view, int position) {
if (optionsSelected == null) {
CardView cardView = (CardView) view;
cardView.setCardBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimarySelected));
optionsSelected = new boolean[recyclerView.getChildCount()];
optionsSelected[position] = true;
fab.hide();
supportInvalidateOptionsMenu();
}
}
}));
}
}
private void deleteCoursesConfirm() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.input_confirm);
builder.setPositiveButton(R.string.input_accept, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
sqlParser = new SQLParser(getApplicationContext());
thread = new Thread(new Runnable() {
public void run() {
ubbDB = sqlParser.getWritableDatabase();
threadHandler.post(new Runnable() {
public void run() {
deleteCourse();
}
});
}
});
thread.start();
}
});
builder.setNegativeButton(R.string.input_cancel, null);
builder.setCancelable(true);
confirmDialog = builder.create();
confirmDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
confirmDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimaryDark));
confirmDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimaryDark));
}
});
confirmDialog.show();
}
private void deleteCourse() {
int options = optionsSelected.length;
List<Course> courses = new ArrayList<>();
for (int i = 0; i < options; i++) {
if (optionsSelected[i]) {
courses.add(courseListAdapter.getCourse(i));
}
}
options = courses.size();
for (int i = 0; i < options; i++) {
Cursor cursor = ubbDB.query(DBClasses.TABLE_NAME, DBClasses.ALL_COLUMNS, DBClasses.COLUMN_COURSE + "=" + courses.get(i).getCourseId(), null, null, null, null);
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
int classId = cursor.getInt(cursor.getColumnIndex(DBClasses.COLUMN_ID));
// Delete the attendance
ubbDB.delete(
DBAttendance.TABLE_NAME,
DBAttendance.COLUMN_CLASS + "=" + classId,
null
);
}
cursor.close();
// Delete the classes
ubbDB.delete(
DBClasses.TABLE_NAME,
DBClasses.COLUMN_COURSE + "=" + courses.get(i).getCourseId(),
null
);
}
// Delete the students that were connected to the course
ubbDB.delete(
DBCoursesStudents.TABLE_NAME,
DBCoursesStudents.COLUMN_COURSE + "=" + courses.get(i).getCourseId(),
null
);
// Delete it from the database
ubbDB.delete(
DBCourses.TABLE_NAME,
DBCourses.COLUMN_ID + "=" + courses.get(i).getCourseId(),
null
);
// Delete it from the adapter
courseListAdapter.deleteCourse(courses.get(i));
}
ubbDB.close();
sqlParser.close();
recyclerView.setAdapter(courseListAdapter);
optionsSelected = null;
fab.show();
supportInvalidateOptionsMenu();
}
private void addCourseListeners() {
// Override the back button so it closes the dialog
addEditCourseDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP && !event.isCanceled()) {
dialog.cancel();
eraseAddEditCourses();
return true;
}
return false;
}
});
// Override the back button
addEditCourseDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Override the standard dismiss onClick to make sure that the window does not close
TextView textView = (TextView) addEditCourseView.findViewById(R.id.edit_year);
int year = (textView.getText().toString().equals("")) ? 0 : Integer.parseInt(textView.getText().toString());
textView = (TextView) addEditCourseView.findViewById(R.id.edit_semester);
int semester = (textView.getText().toString().equals("")) ? 0 : Integer.parseInt(textView.getText().toString());
textView = (TextView) addEditCourseView.findViewById(R.id.edit_section);
int section = (textView.getText().toString().equals("")) ? 0 : Integer.parseInt(textView.getText().toString());
if (String.valueOf(year).length() != 4) {
Toast.makeText(getApplicationContext(), R.string.courses_invalid_year, Toast.LENGTH_SHORT).show();
}
else if (semester < 1 || semester > 2) {
Toast.makeText(getApplicationContext(), R.string.courses_invalid_semester, Toast.LENGTH_SHORT).show();
}
else if (section == 0) {
Toast.makeText(getApplicationContext(), R.string.courses_invalid_section, Toast.LENGTH_SHORT).show();
}
else {
final Course course = new Course(subject, section, semester, year);
sqlParser = new SQLParser(getApplicationContext());
thread = new Thread(new Runnable() {
public void run() {
ubbDB = sqlParser.getWritableDatabase();
threadHandler.post(new Runnable() {
public void run() {
final AddCourse addCourse = new AddCourse(getApplicationContext());
addCourse.execute(course);
progress.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
addCourse.cancel(true);
addEditCourseDialog.dismiss();
finish();
}
});
}
});
}
});
thread.start();
}
}
});
}
private void editCourseListeners() {
// Override the back button so it closes the dialog
addEditCourseDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP && !event.isCanceled()) {
dialog.cancel();
eraseAddEditCourses();
return true;
}
return false;
}
});
// Override the back button
addEditCourseDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Override the standard dismiss onClick to make sure that the window does not close
TextView textView = (TextView) addEditCourseView.findViewById(R.id.edit_year);
int year = (textView.getText().toString().equals("")) ? 0 : Integer.parseInt(textView.getText().toString());
textView = (TextView) addEditCourseView.findViewById(R.id.edit_semester);
int semester = (textView.getText().toString().equals("")) ? 0 : Integer.parseInt(textView.getText().toString());
textView = (TextView) addEditCourseView.findViewById(R.id.edit_section);
int section = (textView.getText().toString().equals("")) ? 0 : Integer.parseInt(textView.getText().toString());
int courseId = 0;
for (int i = 0; i < optionsSelected.length; i++) {
if (optionsSelected[i]) {
courseId = courseListAdapter.getCourse(i).getCourseId();
}
}
if (String.valueOf(year).length() != 4) {
Toast.makeText(getApplicationContext(), R.string.courses_invalid_year, Toast.LENGTH_SHORT).show();
}
else if (semester < 1 || semester > 2) {
Toast.makeText(getApplicationContext(), R.string.courses_invalid_semester, Toast.LENGTH_SHORT).show();
}
else if (section == 0) {
Toast.makeText(getApplicationContext(), R.string.courses_invalid_section, Toast.LENGTH_SHORT).show();
}
else {
final Course course = new Course(subject, courseId, section, semester, year);
sqlParser = new SQLParser(getApplicationContext());
thread = new Thread(new Runnable() {
public void run() {
ubbDB = sqlParser.getWritableDatabase();
threadHandler.post(new Runnable() {
public void run() {
final SaveEditCourse saveEditCourse = new SaveEditCourse(getApplicationContext());
saveEditCourse.execute(course);
progress.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
saveEditCourse.cancel(true);
addEditCourseDialog.dismiss();
finish();
}
});
}
});
}
});
thread.start();
}
}
});
}
private void eraseAddEditCourses() {
TextView textView = (TextView) addEditCourseView.findViewById(R.id.edit_year);
textView.setText("");
textView.requestFocus();
textView = (TextView) addEditCourseView.findViewById(R.id.edit_semester);
textView.setText("");
textView = (TextView) addEditCourseView.findViewById(R.id.edit_section);
textView.setText("");
}
private class EditCourse extends AsyncTask<Void, Void, Course> {
private Context context;
private PowerManager.WakeLock wakeLock;
protected EditCourse(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// Let's make sure the CPU doesn't go to sleep
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
if (!wakeLock.isHeld()) {
wakeLock.acquire();
}
}
@Override
protected Course doInBackground(Void... variable) {
if (isCancelled()) {
return null;
}
Course course = new Course();
for (int i = 0; i < optionsSelected.length; i++) {
if (optionsSelected[i]) {
course = courseListAdapter.getCourse(i);
}
}
Cursor cursor = ubbDB.query(
DBCourses.TABLE_NAME,
DBCourses.ALL_COLUMNS,
DBCourses.COLUMN_ID + "=" + course.getCourseId(),
null,
null,
null,
null,
"1");
cursor.moveToFirst();
course = new Course(
subject,
cursor.getInt(cursor.getColumnIndex(DBCourses.COLUMN_ID)),
cursor.getInt(cursor.getColumnIndex(DBCourses.COLUMN_SECTION)),
cursor.getInt(cursor.getColumnIndex(DBCourses.COLUMN_SEMESTER)),
cursor.getInt(cursor.getColumnIndex(DBCourses.COLUMN_YEAR))
);
cursor.close();
// Close the database connection
ubbDB.close();
sqlParser.close();
return course;
}
@Override
protected void onPostExecute(Course course) {
// Release the kraken errr wakelock
wakeLock.release();
addEditCourseDialog.setTitle(R.string.courses_edit_course);
TextView textView = (TextView) addEditCourseView.findViewById(R.id.edit_year);
textView.setText(String.valueOf(course.getYear()));
textView = (TextView) addEditCourseView.findViewById(R.id.edit_semester);
textView.setText(String.valueOf(course.getCourseSemester()));
textView = (TextView) addEditCourseView.findViewById(R.id.edit_section);
textView.setText(String.valueOf(course.getCourseSection()));
addEditCourseDialog.show();
editCourseListeners();
}
}
private class SaveEditCourse extends AsyncTask<Course, Course, Boolean> {
private Context context;
private PowerManager.WakeLock wakeLock;
protected SaveEditCourse(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// Let's make sure the CPU doesn't go to sleep
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
if (!wakeLock.isHeld()) {
wakeLock.acquire();
}
}
@Override
protected Boolean doInBackground(Course... courses) {
for (Course course : courses) {
if (isCancelled()) {
return false;
}
Cursor cursor = ubbDB.query(DBCourses.TABLE_NAME,
DBCourses.ALL_COLUMNS, DBCourses.COLUMN_SECTION + "=" + course.getCourseSection() + " AND " + DBCourses.COLUMN_YEAR + "=" + course.getYear() + " AND " + DBCourses.COLUMN_SEMESTER + "=" + course.getCourseSemester() + " AND " + DBCourses.COLUMN_SUBJECT + "=" + course.getSubjectId(),
null,
null,
null,
null,
"1");
if (cursor.getCount() == 0) {
ContentValues values = new ContentValues();
values.put(DBCourses.COLUMN_SECTION, course.getCourseSection());
values.put(DBCourses.COLUMN_SEMESTER, course.getCourseSemester());
values.put(DBCourses.COLUMN_YEAR, course.getYear());
ubbDB.update(DBCourses.TABLE_NAME,
values,
DBCourses.COLUMN_ID + "=" + course.getCourseId(),
null);
ubbDB.close();
sqlParser.close();
}
else {
return true;
}
cursor.close();
}
publishProgress(courses);
return false;
}
@Override
protected void onProgressUpdate(Course... courses) {
// Add the course to the adapter
for (Course course : courses) {
int index;
for (index = 0; index < optionsSelected.length; index++) {
if (optionsSelected[index]) {
break;
}
}
courseListAdapter.updateCourse(index, course);
}
}
@Override
protected void onPostExecute(Boolean result) {
// Release the kraken errr wakelock
if (result) {
Toast.makeText(context, R.string.courses_course_exists, Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(context, R.string.courses_edited, Toast.LENGTH_SHORT).show();
}
wakeLock.release();
addEditCourseDialog.dismiss();
// Update the adapter list to show
for (int i = 0; i < optionsSelected.length; i++) {
if (optionsSelected[i]) {
CardView cardView = (CardView) recyclerView.getChildAt(i);
cardView.setCardBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimaryDark));
}
}
recyclerView.swapAdapter(courseListAdapter, true);
optionsSelected = null;
invalidateOptionsMenu();
fab.show();
eraseAddEditCourses();
}
}
private class AddCourse extends AsyncTask<Course, Course, Boolean> {
private Context context;
private PowerManager.WakeLock wakeLock;
protected AddCourse(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// Let's make sure the CPU doesn't go to sleep
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
if (!wakeLock.isHeld()) {
wakeLock.acquire();
}
}
@Override
protected Boolean doInBackground(Course... courses) {
for (Course course : courses) {
if (isCancelled()) {
return false;
}
Cursor cursor = ubbDB.query(DBCourses.TABLE_NAME,
DBCourses.ALL_COLUMNS, DBCourses.COLUMN_SECTION + "=" + course.getCourseSection() + " AND " + DBCourses.COLUMN_YEAR + "=" + course.getYear() + " AND " + DBCourses.COLUMN_SEMESTER + "=" + course.getCourseSemester() + " AND " + DBCourses.COLUMN_SUBJECT + "=" + course.getSubjectId(),
null,
null,
null,
null,
"1");
if (cursor.getCount() == 0) {
ContentValues values = new ContentValues();
values.put(DBCourses.COLUMN_SUBJECT, course.getSubjectId());
values.put(DBCourses.COLUMN_SECTION, course.getCourseSection());
values.put(DBCourses.COLUMN_SEMESTER, course.getCourseSemester());
values.put(DBCourses.COLUMN_YEAR, course.getYear());
ubbDB.insert(DBCourses.TABLE_NAME, null, values);
}
else {
return true;
}
cursor.close();
ubbDB.close();
sqlParser.close();
}
publishProgress(courses);
return false;
}
@Override
protected void onProgressUpdate(Course... courses) {
// Add the course to the adapter
for (Course course : courses) {
courseListAdapter.addCourse(course);
}
}
@Override
protected void onPostExecute(Boolean result) {
// Release the kraken errr wakelock
if (result) {
Toast.makeText(context, R.string.courses_course_exists, Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(context, R.string.courses_added, Toast.LENGTH_SHORT).show();
}
wakeLock.release();
addEditCourseDialog.dismiss();
// Update the adapter list to show
recyclerView.swapAdapter(courseListAdapter, true);
eraseAddEditCourses();
}
}
}

View File

@@ -0,0 +1,80 @@
package cl.cromer.ubb.attendance;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
public class CourseListAdapter extends RecyclerView.Adapter<CourseListAdapter.CourseViewHolder> {
private List<Course> courses;
public CourseListAdapter(List<Course> courses) {
this.courses = courses;
}
@Override
public int getItemCount() {
return courses.size();
}
@Override
public CourseViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.view_course_list_card, viewGroup, false);
return new CourseViewHolder(view);
}
@Override
public void onBindViewHolder(CourseViewHolder courseViewHolder, int i) {
courseViewHolder.courseYear.setText(String.valueOf(courses.get(i).getYear()));
courseViewHolder.courseSemester.setText(String.valueOf(courses.get(i).getCourseSemester()));
courseViewHolder.courseSection.setText(String.valueOf(courses.get(i).getCourseSection()));
courseViewHolder.courseId.setText(String.valueOf(courses.get(i).getCourseId()));
courseViewHolder.itemView.setLongClickable(true);
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
public void addCourse(Course course) {
courses.add(getItemCount(), course);
notifyItemInserted(getItemCount());
}
public void updateCourse(int index, Course course) {
courses.set(index, course);
notifyItemChanged(index);
}
public Course getCourse(int index) {
return courses.get(index);
}
public void deleteCourse(Course course) {
courses.remove(course);
}
public static class CourseViewHolder extends RecyclerView.ViewHolder {
public CardView cardView;
public TextView courseYear;
public TextView courseSemester;
public TextView courseSection;
public TextView courseId;
public CourseViewHolder(View itemView) {
super(itemView);
cardView = (CardView) itemView.findViewById(R.id.course_card_view);
courseYear = (TextView) itemView.findViewById(R.id.course_year);
courseSemester = (TextView) itemView.findViewById(R.id.course_semester);
courseSection = (TextView) itemView.findViewById(R.id.course_section);
courseId = (TextView) itemView.findViewById(R.id.course_id);
}
}
}

View File

@@ -0,0 +1,867 @@
package cl.cromer.ubb.attendance;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.PowerManager;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.Toast;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import cl.cromer.ubb.attendance.Progress;
import cl.cromer.ubb.attendance.DBSchema.*;
public class CourseReport extends AppCompatActivity {
// SQLite database
private SQLParser sqlParser = null;
private SQLiteDatabase ubbDB = null;
// Background thread for the database
private Thread thread = null;
private Handler threadHandler = new Handler();
// Progress bar
private Progress progress = null;
private AlertDialog choiceDialog = null;
private AlertDialog confirmDialog = null;
private View reportView;
private List<Subject> subjects = new ArrayList<>();
private List<Course> courses = new ArrayList<>();
private List<Class> classes = new ArrayList<>();
private List<Attendance> students = new ArrayList<>();
private Course course = new Course();
private int disabledButtonColor;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_class_report);
this.context = this;
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
reportView = inflater.inflate(R.layout.view_course_report, new RelativeLayout(this), false);
// Build the add subject dialog window using the subject view
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(reportView);
builder.setPositiveButton(R.string.input_accept, null);
builder.setNegativeButton(R.string.input_cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
choiceDialog = builder.create();
choiceDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
ColorStateList colorStateList = choiceDialog.getButton(AlertDialog.BUTTON_POSITIVE).getTextColors();
choiceDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
choiceDialog.getButton(AlertDialog.BUTTON_POSITIVE).setClickable(false);
choiceDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimaryDark));
if (colorStateList != null) {
disabledButtonColor = colorStateList.getColorForState(new int[] {-android.R.attr.state_enabled}, R.color.colorPrimary);
}
}
});
choiceDialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
choiceDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
finish();
}
});
choiceDialog.setCanceledOnTouchOutside(false);
// Create a progress dialog for slow devices
progress = new Progress();
progress.show(this, 1);
progress.setCancelable(false);
// Load the SQLite database
sqlParser = new SQLParser(this);
thread = new Thread(new Runnable() {
public void run() {
ubbDB = sqlParser.getWritableDatabase();
threadHandler.post(new Runnable() {
public void run() {
databaseLoaded();
}
});
}
});
thread.start();
progress.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
thread.interrupt();
finish();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
// We need to get rid of the progressbar if it's showing
if (progress != null && progress.isShowing()) {
progress.dismiss();
}
if (choiceDialog != null && choiceDialog.isShowing()) {
choiceDialog.dismiss();
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
private void databaseLoaded() {
final GetSubjects getSubjects = new GetSubjects(getApplicationContext());
getSubjects.execute();
progress.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
getSubjects.cancel(true);
finish();
}
});
}
private AdapterView.OnItemSelectedListener subjectListener() {
return new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
courses = new ArrayList<>();
if (position == 0) {
choiceDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(disabledButtonColor);
choiceDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
choiceDialog.getButton(AlertDialog.BUTTON_POSITIVE).setClickable(false);
Spinner spinner = (Spinner) reportView.findViewById(R.id.course_spinner);
List<String> options = new ArrayList<>();
options.add(getResources().getString(R.string.report_course));
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(getApplicationContext(), R.layout.view_spinner_selected_item, options);
arrayAdapter.setDropDownViewResource(R.layout.view_spinner_dropdown_item);
spinner.setAdapter(arrayAdapter);
spinner.setSelection(0);
spinner.setEnabled(false);
spinner.setClickable(false);
spinner.setOnItemSelectedListener(null);
}
else {
progress = new Progress();
progress.show(context, 1);
progress.setCancelable(false);
final int thePosition = position - 1;
course.setSubject(subjects.get(thePosition));
sqlParser = new SQLParser(getApplicationContext());
thread = new Thread(new Runnable() {
public void run() {
ubbDB = sqlParser.getWritableDatabase();
threadHandler.post(new Runnable() {
public void run() {
final GetCourses getCourses = new GetCourses(getApplicationContext());
getCourses.execute(thePosition);
progress.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
getCourses.cancel(true);
finish();
}
});
}
});
}
});
thread.start();
progress.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
thread.interrupt();
finish();
}
});
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
}
private AdapterView.OnItemSelectedListener courseListener() {
return new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
choiceDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(disabledButtonColor);
choiceDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
choiceDialog.getButton(AlertDialog.BUTTON_POSITIVE).setClickable(false);
choiceDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(null);
}
else {
course.setCourse(courses.get(position - 1));
choiceDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimaryDark));
choiceDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
choiceDialog.getButton(AlertDialog.BUTTON_POSITIVE).setClickable(true);
choiceDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(acceptListener());
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
}
private View.OnClickListener acceptListener() {
return new View.OnClickListener() {
@Override
public void onClick(View view) {
progress = new Progress();
progress.show(context, 1);
progress.setCancelable(false);
choiceDialog.dismiss();
sqlParser = new SQLParser(getApplicationContext());
thread = new Thread(new Runnable() {
public void run() {
ubbDB = sqlParser.getWritableDatabase();
threadHandler.post(new Runnable() {
public void run() {
final GetClasses getClasses = new GetClasses(getApplicationContext());
getClasses.execute();
progress.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
getClasses.cancel(true);
finish();
}
});
}
});
}
});
thread.start();
progress.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
thread.interrupt();
finish();
}
});
}
};
}
private class GetSubjects extends AsyncTask<Void, Void, Void> {
private Context context;
private PowerManager.WakeLock wakeLock;
protected GetSubjects(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// Let's make sure the CPU doesn't go to sleep
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
if (!wakeLock.isHeld()) {
wakeLock.acquire();
}
}
@Override
protected Void doInBackground(Void... voids) {
if (isCancelled()) {
return null;
}
// Get subjects by name in ascending order
String query = "SELECT " + sqlParser.combineColumnsStrings(DBSubjects.ALL_COLUMNS, DBMajors.ALL_COLUMNS) + " FROM " + DBSubjects.TABLE_NAME + " INNER JOIN (SELECT " + sqlParser.combineColumnsStrings(DBMajors.ALL_COLUMNS) +" FROM " + DBMajors.TABLE_NAME + ") " + DBMajors.TABLE_NAME + " ON " + DBSubjects.TABLE_NAME + "." + DBSubjects.COLUMN_MAJOR + "=" + DBMajors.TABLE_NAME + "." + DBMajors.COLUMN_ID + " ORDER BY " + DBSubjects.COLUMN_NAME + " ASC";
if (BuildConfig.DEBUG) {
Log.d("CourseReport", query);
}
Cursor cursor = ubbDB.rawQuery(query, null);
// Iterate through the database rows
while (cursor.moveToNext()) {
if (isCancelled()) {
return null;
}
Major major = new Major(
cursor.getInt(cursor.getColumnIndex(DBMajors.COLUMN_ID)),
cursor.getString(cursor.getColumnIndex(DBMajors.COLUMN_NAME)),
cursor.getInt(cursor.getColumnIndex(DBMajors.COLUMN_CODE)));
subjects.add(
new Subject(
major,
cursor.getInt(cursor.getColumnIndex(DBSubjects.COLUMN_ID)),
cursor.getString(cursor.getColumnIndex(DBSubjects.COLUMN_NAME)),
cursor.getInt(cursor.getColumnIndex(DBSubjects.COLUMN_CODE))
)
);
}
cursor.close();
// Close the database connection
ubbDB.close();
sqlParser.close();
return null;
}
@Override
protected void onPostExecute(Void result) {
// Release the kraken errr wakelock
List<String> options = new ArrayList<>();
options.add(getResources().getString(R.string.report_subject));
for (Subject subject : subjects) {
options.add(subject.getSubjectName());
}
Spinner spinner = (Spinner) reportView.findViewById(R.id.subject_spinner);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(getApplicationContext(), R.layout.view_spinner_selected_item, options);
arrayAdapter.setDropDownViewResource(R.layout.view_spinner_dropdown_item);
spinner.setAdapter(arrayAdapter);
spinner.setSelection(0);
spinner.setOnItemSelectedListener(subjectListener());
progress.dismiss();
wakeLock.release();
choiceDialog.show();
}
}
private class GetCourses extends AsyncTask<Integer, Void, Void> {
private Context context;
private PowerManager.WakeLock wakeLock;
protected GetCourses(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// Let's make sure the CPU doesn't go to sleep
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
if (!wakeLock.isHeld()) {
wakeLock.acquire();
}
}
@Override
protected Void doInBackground(Integer... position) {
if (isCancelled()) {
return null;
}
Cursor cursor = ubbDB.query(
DBCourses.TABLE_NAME,
DBCourses.ALL_COLUMNS,
DBCourses.COLUMN_SUBJECT + "=" + subjects.get(position[0]).getSubjectId(),
null,
null,
null,
DBCourses.COLUMN_YEAR + " DESC, " + DBCourses.COLUMN_SEMESTER + " DESC, " + DBCourses.COLUMN_SECTION + " ASC",
null);
// Iterate through the database rows
while (cursor.moveToNext()) {
if (isCancelled()) {
return null;
}
courses.add(
new Course(
subjects.get(position[0]),
cursor.getInt(cursor.getColumnIndex(DBCourses.COLUMN_ID)),
cursor.getInt(cursor.getColumnIndex(DBCourses.COLUMN_SECTION)),
cursor.getInt(cursor.getColumnIndex(DBCourses.COLUMN_SEMESTER)),
cursor.getInt(cursor.getColumnIndex(DBCourses.COLUMN_YEAR))
)
);
}
cursor.close();
// Close the database connection
ubbDB.close();
sqlParser.close();
return null;
}
@Override
protected void onPostExecute(Void result) {
// Release the kraken errr wakelock
List<String> options = new ArrayList<>();
options.add(getResources().getString(R.string.report_course));
for (Course course : courses) {
options.add(course.getYear() + "-" + course.getCourseSemester() + " " + getResources().getString(R.string.report_section) + " " + course.getCourseSection());
}
Spinner spinner = (Spinner) reportView.findViewById(R.id.course_spinner);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(getApplicationContext(), R.layout.view_spinner_selected_item, options);
arrayAdapter.setDropDownViewResource(R.layout.view_spinner_dropdown_item);
spinner.setAdapter(arrayAdapter);
spinner.setSelection(0);
spinner.setEnabled(true);
spinner.setClickable(true);
spinner.setOnItemSelectedListener(courseListener());
progress.dismiss();
wakeLock.release();
}
}
private class GetClasses extends AsyncTask<Integer, Void, Boolean> {
private Context context;
private PowerManager.WakeLock wakeLock;
protected GetClasses(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// Let's make sure the CPU doesn't go to sleep
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
if (!wakeLock.isHeld()) {
wakeLock.acquire();
}
}
@Override
protected Boolean doInBackground(Integer... position) {
if (isCancelled()) {
return false;
}
Cursor cursor = ubbDB.query(
DBClasses.TABLE_NAME,
DBClasses.ALL_COLUMNS,
DBClasses.COLUMN_COURSE + "=" + course.getCourseId(),
null,
null,
null,
DBClasses.COLUMN_DATE + " DESC",
null);
// Iterate through the database rows
while (cursor.moveToNext()) {
if (isCancelled()) {
return false;
}
classes.add(
new Class(
course,
cursor.getInt(cursor.getColumnIndex(DBClasses.COLUMN_ID)),
cursor.getLong(cursor.getColumnIndex(DBClasses.COLUMN_DATE))
)
);
}
cursor.close();
String query = "SELECT " + sqlParser.combineColumnsStrings(DBStudents.ALL_COLUMNS) + " FROM " + DBStudents.TABLE_NAME +
" INNER JOIN (SELECT " + sqlParser.combineColumnsStrings(DBCoursesStudents.ALL_COLUMNS) + " FROM " + DBCoursesStudents.TABLE_NAME + " WHERE " + DBCoursesStudents.COLUMN_COURSE + "=" + course.getCourseId() + ") AS " + DBCoursesStudents.TABLE_NAME + " ON " + DBStudents.TABLE_NAME + "." + DBStudents.COLUMN_ID + "=" + DBCoursesStudents.TABLE_NAME + "." + DBCoursesStudents.COLUMN_STUDENT +
" ORDER BY " + DBStudents.COLUMN_FIRST_LAST_NAME + " ASC, " + DBStudents.COLUMN_FIRST_NAME + " ASC";
if (BuildConfig.DEBUG) {
Log.d("CourseReport", query);
}
cursor = ubbDB.rawQuery(query, null);
// Iterate through the database rows
while (cursor.moveToNext()) {
if (isCancelled()) {
return false;
}
students.add(new Attendance(
0,
cursor.getInt(cursor.getColumnIndex(DBStudents.COLUMN_ID)),
cursor.getString(cursor.getColumnIndex(DBStudents.COLUMN_RUN)),
cursor.getString(cursor.getColumnIndex(DBStudents.COLUMN_FIRST_NAME)),
cursor.getString(cursor.getColumnIndex(DBStudents.COLUMN_SECOND_NAME)),
cursor.getString(cursor.getColumnIndex(DBStudents.COLUMN_FIRST_LAST_NAME)),
cursor.getString(cursor.getColumnIndex(DBStudents.COLUMN_SECOND_LAST_NAME)),
cursor.getInt(cursor.getColumnIndex(DBStudents.COLUMN_MAJOR)),
cursor.getInt(cursor.getColumnIndex(DBStudents.COLUMN_ENROLLED)),
cursor.getString(cursor.getColumnIndex(DBStudents.COLUMN_EMAIL)),
cursor.getBlob(cursor.getColumnIndex(DBStudents.COLUMN_PHOTO))
));
}
cursor.close();
for (Class classObject : classes) {
if (isCancelled()) {
return false;
}
cursor = ubbDB.query(
DBAttendance.TABLE_NAME,
DBAttendance.ALL_COLUMNS,
DBAttendance.COLUMN_CLASS + "=" + classObject.getClassId(),
null,
null,
null,
null,
null);
while (cursor.moveToNext()) {
if (isCancelled()) {
return false;
}
int studentId = cursor.getInt(cursor.getColumnIndex(DBAttendance.COLUMN_STUDENT));
int status = cursor.getInt(cursor.getColumnIndex(DBAttendance.COLUMN_ATTENDANCE));
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getStudentId() == studentId) {
// 0 - uninitialized
// 1 - present
// 2 - justified
// 3 - absent
// 4 - late
switch (status) {
case 1:
students.get(i).setPresent(students.get(i).getPresent() + 1);
break;
case 2:
students.get(i).setJustified(students.get(i).getJustified() + 1);
break;
case 3:
students.get(i).setAbsent(students.get(i).getAbsent() + 1);
break;
case 4:
students.get(i).setLate(students.get(i).getLate() + 1);
break;
}
break;
}
}
}
cursor.close();
}
Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet(getResources().getString(R.string.app_name));
Excel excel = new Excel(workbook);
excel.createStyles();
Row row;
Cell cell;
// Major
row = sheet.createRow(0);
cell = row.createCell(0);
cell.setCellValue(course.getMajorName() + " - " + course.getMajorCode());
cell.setCellStyle(excel.getCellStyle("left"));
for (int i = 1; i < 7; i++) {
cell = row.createCell(i);
if (i == 6) {
cell.setCellStyle(excel.getCellStyle("right"));
}
else {
cell.setCellStyle(excel.getCellStyle("middle"));
}
}
// Subject
row = sheet.createRow(1);
cell = row.createCell(0);
cell.setCellValue(course.getSubjectName() + " - " + course.getSubjectCode());
cell.setCellStyle(excel.getCellStyle("left"));
for (int i = 1; i < 7; i++) {
cell = row.createCell(i);
if (i == 6) {
cell.setCellStyle(excel.getCellStyle("right"));
}
else {
cell.setCellStyle(excel.getCellStyle("middle"));
}
}
// Number of classes
row = sheet.createRow(2);
cell = row.createCell(0);
cell.setCellValue(classes.size() + " " + getResources().getString(R.string.report_classes));
cell.setCellStyle(excel.getCellStyle("left"));
for (int i = 1; i < 7; i++) {
cell = row.createCell(i);
if (i == 6) {
cell.setCellStyle(excel.getCellStyle("right"));
}
else {
cell.setCellStyle(excel.getCellStyle("middle"));
}
}
// Columns
row = sheet.createRow(3);
cell = row.createCell(0);
cell.setCellValue(getResources().getString(R.string.report_names));
cell.setCellStyle(excel.getCellStyle("blue"));
cell = row.createCell(1);
cell.setCellValue(getResources().getString(R.string.report_first_last_name));
cell.setCellStyle(excel.getCellStyle("blue"));
cell = row.createCell(2);
cell.setCellValue(getResources().getString(R.string.report_second_last_name));
cell.setCellStyle(excel.getCellStyle("blue"));
cell = row.createCell(3);
cell.setCellValue(getResources().getString(R.string.report_present));
cell.setCellStyle(excel.getCellStyle("green"));
cell = row.createCell(4);
cell.setCellValue(getResources().getString(R.string.report_late));
cell.setCellStyle(excel.getCellStyle("yellow"));
cell = row.createCell(5);
cell.setCellValue(getResources().getString(R.string.report_justified));
cell.setCellStyle(excel.getCellStyle("orange"));
cell = row.createCell(6);
cell.setCellValue(getResources().getString(R.string.report_absent));
cell.setCellStyle(excel.getCellStyle("red"));
int cellSize1 = getResources().getString(R.string.report_names).length();
int cellSize2 = getResources().getString(R.string.report_first_last_name).length();
int cellSize3 = getResources().getString(R.string.report_second_last_name).length();
int rowNumber = 4;
String value;
for (Attendance student : students) {
row = sheet.createRow(rowNumber);
// First and second name
cell = row.createCell(0);
value = student.getFirstName() + " " + student.getSecondName();
if (value.length() > cellSize1) {
cellSize1 = value.length();
}
cell.setCellValue(value);
cell.setCellStyle(excel.getCellStyle("main"));
// First last name
cell = row.createCell(1);
value = student.getFirstLastName();
if (value.length() > cellSize2) {
cellSize2 = value.length();
}
cell.setCellValue(value);
cell.setCellStyle(excel.getCellStyle("main"));
// Second last name
cell = row.createCell(2);
value = student.getSecondLastName();
if (value.length() > cellSize3) {
cellSize3 = value.length();
}
cell.setCellValue(value);
cell.setCellStyle(excel.getCellStyle("main"));
for (int i = 3; i < 7; i++) {
cell = row.createCell(i);
switch (i) {
case 3:
cell.setCellStyle(excel.getCellStyle("green"));
cell.setCellValue(student.getPresent());
break;
case 4:
cell.setCellStyle(excel.getCellStyle("yellow"));
cell.setCellValue(student.getLate());
break;
case 5:
cell.setCellStyle(excel.getCellStyle("orange"));
cell.setCellValue(student.getJustified());
break;
case 6:
cell.setCellStyle(excel.getCellStyle("red"));
cell.setCellValue(student.getAbsent());
break;
}
}
rowNumber++;
}
sheet.setColumnWidth(0, (cellSize1 + 1) * 256);
sheet.setColumnWidth(1, (cellSize2 + 1) * 256);
sheet.setColumnWidth(2, (cellSize3 + 1) * 256);
final String filePath = Environment.getExternalStorageDirectory() +
"/" + getResources().getString(R.string.app_name) +
"/" + course.getMajorName();
final String fileName = "/" + getResources().getString(R.string.report_course) + "-" + course.getYear() + "-" + course.getCourseSemester() + "-" + course.getCourseSection() + ".xls";
try {
File file = new File(filePath);
if (!file.exists()) {
if (!file.mkdirs()) {
return false;
}
}
FileOutputStream fileOutputStream = new FileOutputStream(filePath + fileName);
workbook.write(fileOutputStream);
fileOutputStream.close();
}
catch (FileNotFoundException e) {
if (BuildConfig.DEBUG) {
e.printStackTrace();
}
return false;
}
catch (IOException e) {
if (BuildConfig.DEBUG) {
e.printStackTrace();
}
return false;
}
// Close the database connection
ubbDB.close();
sqlParser.close();
return true;
}
@Override
protected void onPostExecute(Boolean result) {
// Release the kraken errr wakelock
if (result) {
confirmOpen();
}
else {
Toast.makeText(getApplicationContext(), R.string.report_fail, Toast.LENGTH_SHORT).show();
finish();
}
progress.dismiss();
wakeLock.release();
}
}
private void confirmOpen() {
final String filePath = Environment.getExternalStorageDirectory() +
"/" + getResources().getString(R.string.app_name) +
"/" + course.getMajorName();
final String fileName = "/" + getResources().getString(R.string.report_course) + "-" + course.getYear() + "-" + course.getCourseSemester() + "-" + course.getCourseSection() + ".xls";
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(R.string.report_created);
builder.setPositiveButton(R.string.input_yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
File file = new File(filePath + fileName);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.ms-excel");
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(CourseReport.this, "No Application Available to View Excel", Toast.LENGTH_SHORT).show();
}
confirmDialog.dismiss();
finish();
}
});
builder.setNegativeButton(R.string.input_no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
confirmDialog.dismiss();
finish();
}
});
builder.setCancelable(true);
confirmDialog = builder.create();
confirmDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
confirmDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimaryDark));
confirmDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimaryDark));
}
});
confirmDialog.show();
}
}

View File

@@ -0,0 +1,80 @@
package cl.cromer.ubb.attendance;
import android.provider.BaseColumns;
final public class DBSchema {
// Disable the constructor
private DBSchema() {}
protected static abstract class DBMajors implements BaseColumns {
protected static final String TABLE_NAME = "majors";
protected static final String COLUMN_ID = "major_id";
protected static final String COLUMN_CODE = "major_code";
protected static final String COLUMN_NAME = "major_name";
protected static final String[] ALL_COLUMNS = {COLUMN_ID, COLUMN_CODE, COLUMN_NAME};
}
protected static abstract class DBSubjects implements BaseColumns {
protected static final String TABLE_NAME = "subjects";
protected static final String COLUMN_ID = "subject_id";
protected static final String COLUMN_CODE = "subject_code";
protected static final String COLUMN_NAME = "subject_name";
protected static final String COLUMN_MAJOR = "subject_major";
protected static final String[] ALL_COLUMNS = {COLUMN_ID, COLUMN_CODE, COLUMN_NAME, COLUMN_MAJOR};
}
protected static abstract class DBCourses implements BaseColumns {
protected static final String TABLE_NAME = "courses";
protected static final String COLUMN_ID = "course_id";
protected static final String COLUMN_SUBJECT = "course_subject";
protected static final String COLUMN_SECTION = "course_section";
protected static final String COLUMN_SEMESTER = "course_semester";
protected static final String COLUMN_YEAR = "course_year";
protected static final String[] ALL_COLUMNS = {COLUMN_ID, COLUMN_SUBJECT, COLUMN_SECTION, COLUMN_SEMESTER, COLUMN_YEAR};
}
protected static abstract class DBCoursesStudents implements BaseColumns {
protected static final String TABLE_NAME = "courses_students";
protected static final String COLUMN_ID = "cs_id";
protected static final String COLUMN_COURSE = "cs_course";
protected static final String COLUMN_STUDENT = "cs_student";
protected static final String[] ALL_COLUMNS = {COLUMN_ID, COLUMN_COURSE, COLUMN_STUDENT};
}
protected static abstract class DBClasses implements BaseColumns {
protected static final String TABLE_NAME = "classes";
protected static final String COLUMN_ID = "class_id";
protected static final String COLUMN_COURSE = "class_course";
protected static final String COLUMN_DATE = "class_date";
protected static final String[] ALL_COLUMNS = {COLUMN_ID, COLUMN_COURSE, COLUMN_DATE};
}
protected static abstract class DBStudents implements BaseColumns {
protected static final String TABLE_NAME = "students";
protected static final String COLUMN_ID = "student_id";
protected static final String COLUMN_RUN = "student_run";
protected static final String COLUMN_FIRST_NAME = "student_first_name";
protected static final String COLUMN_SECOND_NAME = "student_second_name";
protected static final String COLUMN_FIRST_LAST_NAME = "student_first_last_name";
protected static final String COLUMN_SECOND_LAST_NAME = "student_second_last_name";
protected static final String COLUMN_FIRST_NAME_NORM = "student_first_name_norm";
protected static final String COLUMN_SECOND_NAME_NORM = "student_second_name_norm";
protected static final String COLUMN_FIRST_LAST_NAME_NORM = "student_first_last_name_norm";
protected static final String COLUMN_SECOND_LAST_NAME_NORM = "student_second_last_name_norm";
protected static final String COLUMN_MAJOR = "student_major";
protected static final String COLUMN_EMAIL = "student_email";
protected static final String COLUMN_ENROLLED = "student_enrolled";
protected static final String COLUMN_PHOTO = "student_photo";
protected static final String[] ALL_COLUMNS = {COLUMN_ID, COLUMN_RUN, COLUMN_FIRST_NAME, COLUMN_SECOND_NAME, COLUMN_FIRST_LAST_NAME, COLUMN_SECOND_LAST_NAME, COLUMN_MAJOR, COLUMN_EMAIL, COLUMN_ENROLLED, COLUMN_PHOTO};
}
protected static abstract class DBAttendance implements BaseColumns {
protected static final String TABLE_NAME = "attendance";
protected static final String COLUMN_ID = "attendance_id";
protected static final String COLUMN_CLASS = "attendance_class";
protected static final String COLUMN_STUDENT = "attendance_student";
protected static final String COLUMN_ATTENDANCE = "attendance_attendance";
protected static final String[] ALL_COLUMNS = {COLUMN_ID, COLUMN_CLASS, COLUMN_STUDENT, COLUMN_ATTENDANCE};
}
}

View File

@@ -0,0 +1,171 @@
package cl.cromer.ubb.attendance;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Workbook;
public class Excel {
private Workbook workbook;
private CellStyle mainCellStyle = null;
private CellStyle leftCellStyle = null;
private CellStyle rightCellStyle = null;
private CellStyle middleCellStyle = null;
private CellStyle blueCellStyle = null;
private CellStyle greenCellStyle = null;
private CellStyle yellowCellStyle = null;
private CellStyle orangeCellStyle = null;
private CellStyle redCellStyle = null;
public Excel(Workbook workbook) {
this.workbook = workbook;
}
protected void createStyles() {
Font font = workbook.createFont();
mainCellStyle = workbook.createCellStyle();
mainCellStyle.setBorderBottom(CellStyle.BORDER_THIN);
mainCellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
mainCellStyle.setBorderLeft(CellStyle.BORDER_THIN);
mainCellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
mainCellStyle.setBorderRight(CellStyle.BORDER_THIN);
mainCellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
mainCellStyle.setBorderTop(CellStyle.BORDER_THIN);
mainCellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
leftCellStyle = workbook.createCellStyle();
leftCellStyle.setBorderBottom(CellStyle.BORDER_THIN);
leftCellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
leftCellStyle.setBorderLeft(CellStyle.BORDER_THIN);
leftCellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
leftCellStyle.setBorderTop(CellStyle.BORDER_THIN);
leftCellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
leftCellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());
leftCellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
font.setColor(HSSFColor.WHITE.index);
leftCellStyle.setFont(font);
rightCellStyle = workbook.createCellStyle();
rightCellStyle.setBorderBottom(CellStyle.BORDER_THIN);
rightCellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
rightCellStyle.setBorderRight(CellStyle.BORDER_THIN);
rightCellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
rightCellStyle.setBorderTop(CellStyle.BORDER_THIN);
rightCellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
rightCellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());
rightCellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
font.setColor(HSSFColor.WHITE.index);
rightCellStyle.setFont(font);
middleCellStyle = workbook.createCellStyle();
middleCellStyle.setBorderBottom(CellStyle.BORDER_THIN);
middleCellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
middleCellStyle.setBorderTop(CellStyle.BORDER_THIN);
middleCellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
middleCellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());
middleCellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
font.setColor(HSSFColor.WHITE.index);
middleCellStyle.setFont(font);
blueCellStyle = workbook.createCellStyle();
blueCellStyle.setBorderBottom(CellStyle.BORDER_THIN);
blueCellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
blueCellStyle.setBorderLeft(CellStyle.BORDER_THIN);
blueCellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
blueCellStyle.setBorderRight(CellStyle.BORDER_THIN);
blueCellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
blueCellStyle.setBorderTop(CellStyle.BORDER_THIN);
blueCellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
blueCellStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
blueCellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
font.setColor(HSSFColor.WHITE.index);
blueCellStyle.setFont(font);
greenCellStyle = workbook.createCellStyle();
greenCellStyle.setBorderBottom(CellStyle.BORDER_THIN);
greenCellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
greenCellStyle.setBorderLeft(CellStyle.BORDER_THIN);
greenCellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
greenCellStyle.setBorderRight(CellStyle.BORDER_THIN);
greenCellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
greenCellStyle.setBorderTop(CellStyle.BORDER_THIN);
greenCellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
greenCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
greenCellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
font.setColor(HSSFColor.BLACK.index);
greenCellStyle.setFont(font);
greenCellStyle.setAlignment(CellStyle.ALIGN_CENTER);
yellowCellStyle = workbook.createCellStyle();
yellowCellStyle.setBorderBottom(CellStyle.BORDER_THIN);
yellowCellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
yellowCellStyle.setBorderLeft(CellStyle.BORDER_THIN);
yellowCellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
yellowCellStyle.setBorderRight(CellStyle.BORDER_THIN);
yellowCellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
yellowCellStyle.setBorderTop(CellStyle.BORDER_THIN);
yellowCellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
yellowCellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
yellowCellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
font.setColor(HSSFColor.BLACK.index);
yellowCellStyle.setFont(font);
yellowCellStyle.setAlignment(CellStyle.ALIGN_CENTER);
orangeCellStyle = workbook.createCellStyle();
orangeCellStyle.setBorderBottom(CellStyle.BORDER_THIN);
orangeCellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
orangeCellStyle.setBorderLeft(CellStyle.BORDER_THIN);
orangeCellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
orangeCellStyle.setBorderRight(CellStyle.BORDER_THIN);
orangeCellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
orangeCellStyle.setBorderTop(CellStyle.BORDER_THIN);
orangeCellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
orangeCellStyle.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
orangeCellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
font.setColor(HSSFColor.BLACK.index);
orangeCellStyle.setFont(font);
orangeCellStyle.setAlignment(CellStyle.ALIGN_CENTER);
redCellStyle = workbook.createCellStyle();
redCellStyle.setBorderBottom(CellStyle.BORDER_THIN);
redCellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
redCellStyle.setBorderLeft(CellStyle.BORDER_THIN);
redCellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
redCellStyle.setBorderRight(CellStyle.BORDER_THIN);
redCellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
redCellStyle.setBorderTop(CellStyle.BORDER_THIN);
redCellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
redCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
redCellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
font.setColor(IndexedColors.BLACK.getIndex());
redCellStyle.setFont(font);
redCellStyle.setAlignment(CellStyle.ALIGN_CENTER);
}
protected CellStyle getCellStyle(String type) {
switch (type) {
case "main":
return mainCellStyle;
case "left":
return leftCellStyle;
case "middle":
return middleCellStyle;
case "right":
return rightCellStyle;
case "blue":
return blueCellStyle;
case "green":
return greenCellStyle;
case "yellow":
return yellowCellStyle;
case "orange":
return orangeCellStyle;
case "red":
return redCellStyle;
default:
return null;
}
}
}

View File

@@ -0,0 +1,264 @@
package cl.cromer.ubb.attendance;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.os.PowerManager;
import android.webkit.MimeTypeMap;
import android.widget.Toast;
import cl.cromer.ubb.attendance.Progress;
/**
* This class handles reading and writing to the file system.
* @author Chris Cromer
* Copyright 2013 -2016
*/
public class FileSystem {
/**
* This method checks the memory card to see if it is mounted and writable.
* @return boolean Returns true if readable or false if it's not.
*/
public static boolean isWritable() {
return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
}
/**
* This method checks the memory card to see if it is readable.
* @return boolean Returns true if it's readable or false if it's not.
*/
public static boolean isReadable() {
return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(Environment.getExternalStorageState());
}
/**
* This method checks if a file exists on the memory card.
* @param context The context of the calling activity.
* @param fileName The name of the file that you want to see if it exists.
* @return boolean Returns true if it exists or false if it doesn't.
*/
public static boolean fileExists(Context context, String fileName) {
File file = new File(context.getExternalFilesDir(null), fileName);
return file.isFile();
}
/**
* This method returns the mime type of the file based off it's extension
* @param url The url of the file to check
* @return String returns the mime type or null if it is invalid
*/
public static String getMimeType(String url) {
String type = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
if (extension != null) {
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
return type;
}
/**
* This method attempts to open a file for viewing/editing using a 3rd party application.
* This method shows a toast if there isn't an application installed for the mime type.
* @param context The context of the activity that wants to open the file.
* @param fileName The name of the file you want to open.
* @param mimeType The mime type of the file you want to open.
*/
public void openFile(Context context, String fileName, String mimeType) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
File file = new File(context.getExternalFilesDir(null), fileName);
intent.setDataAndType(Uri.fromFile(file), mimeType);
final PackageManager packageManager = context.getPackageManager();
List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
if (list.size() == 0) {
Toast.makeText(context, context.getString(R.string.general_no_app), Toast.LENGTH_SHORT).show();
}
else {
context.startActivity(intent);
}
}
public boolean renameFile(Context context, String oldFileName, String newFileName) {
if (fileExists(context, oldFileName)) {
File oldFile = new File(context.getExternalFilesDir(null), oldFileName);
File newFile = new File(context.getExternalFilesDir(null), newFileName);
return oldFile.renameTo(newFile);
}
return false;
}
/**
* This method deletes the file passed
* @param context The context of the application.
* @param fileName The name of the file you want to delete.
* @return boolean True on success or false if it fails.
*/
public boolean deleteFile(Context context, String fileName) {
if (fileExists(context, fileName)) {
File file = new File(context.getExternalFilesDir(null), fileName);
return file.delete();
}
return false;
}
/**
* This method returns the size of the file.
* @param context The context of the application.
* @param fileName The name of the file you want the size of.
* @return long The size of the file in bytes.
*/
public long fileSize(Context context, String fileName) {
if (fileExists(context, fileName)) {
File file = new File(context.getExternalFilesDir(null), fileName);
return file.length();
}
return 0;
}
/**
* This method returns the file path based on the version of android that is in use.
* @param context The context of the application.
* @param fileName The name of the file you want to get the path for.
* @return String This string contains the full path to the file.
*/
public String getFilePath(Context context, String fileName) {
return context.getExternalFilesDir(null) + "/" + fileName;
}
/**
* This class is an AsycnTask used to download a file to the memory card.
* The construct requires 2 fields. Context and Filename.
* In the execute command; a URL is needed to download.
* This class will call a listener if you pass one through downloadListener.
* @author cromer
*/
public class Download extends AsyncTask<String, Integer, Boolean> {
private Context context;
private PowerManager.WakeLock wakeLock;
private String fileName;
public Progress progress = null; // If this is set the progress bar will be updated
public ListenerDownload downloadListener = null; // This needs to be set from the parent activity
/**
* This construct is used to configure the class to run.
* @param context The context of the parent activity.
* @param fileName The name of the file you want to save to the memory card.
*/
public Download(Context context, String fileName) {
this.context = context;
this.fileName = fileName;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// Let's make sure the CPU doesn't go to sleep
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
if (!wakeLock.isHeld()) {
wakeLock.acquire();
}
}
@Override
protected Boolean doInBackground(String... receivedUrl) {
InputStream inStream;
OutputStream outStream;
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork == null) {
// Couldn't get a connection
return false;
}
if (!activeNetwork.isConnected()) {
// Not connected
return false;
}
try {
URL url = new URL(receivedUrl[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(10000);
connection.setConnectTimeout(15000);
connection.connect();
// Problem with the server and file
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
return false;
}
int fileLength = connection.getContentLength();
inStream = connection.getInputStream();
File file = new File(context.getExternalFilesDir(null), fileName);
outStream = new FileOutputStream(file);
byte data[] = new byte[4096];
long total = 0;
int count;
while ((count = inStream.read(data)) != -1) {
// They cancelled the download
if (isCancelled()) {
inStream.close();
outStream.close();
return file.delete(); // If they cancelled the download, delete what was downloaded
}
total += count;
if (fileLength > 0) {
// Update the progress meter if we know how large the file is
publishProgress((int) (total * 100 / fileLength));
}
outStream.write(data, 0, count);
}
outStream.close();
inStream.close();
connection.disconnect();
return true;
}
catch (Exception e) {
e.printStackTrace();
}
return false;
}
@Override
protected void onProgressUpdate(Integer... progressUpdate) {
super.onProgressUpdate(progressUpdate);
// if we get here, length is known, now set indeterminate to false and show how long till it's finished
if (progress != null) {
progress.setIndeterminate(false);
progress.setProgress(progressUpdate[0]);
}
}
@Override
protected void onPostExecute(Boolean result) {
wakeLock.release();
if (downloadListener != null) {
// Call the listener if one was set
downloadListener.onDownloadComplete(result);
}
}
}
}

View File

@@ -0,0 +1,751 @@
package cl.cromer.ubb.attendance;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.PowerManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import cl.cromer.ubb.attendance.Progress;
import cl.cromer.ubb.attendance.RUT;
import cl.cromer.ubb.attendance.DBSchema.DBSubjects;
import cl.cromer.ubb.attendance.DBSchema.DBMajors;
import cl.cromer.ubb.attendance.DBSchema.DBStudents;
import cl.cromer.ubb.attendance.DBSchema.DBCourses;
import cl.cromer.ubb.attendance.DBSchema.DBCoursesStudents;
public class ImportExcel extends AppCompatActivity {
// SQLite database
private SQLParser sqlParser = null;
private SQLiteDatabase ubbDB = null;
// Background thread for the database
private Thread thread = null;
private Handler threadHandler = new Handler();
// Progress bar
private Progress progress = null;
// Background async
private ReadFile readFile = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_import_excel);
// Receive the info about the excel file
Intent subjectListIntent = getIntent();
final int type = subjectListIntent.getIntExtra(StaticVariables.IMPORT_EXCEL_FILE_TYPE, 1);
final String path = subjectListIntent.getStringExtra(StaticVariables.IMPORT_EXCEL_FILE);
// Create a progress dialog for slow devices
progress = new Progress();
progress.show(this, 1);
progress.setCancelable(false);
// Load the SQLite database
sqlParser = new SQLParser(this);
thread = new Thread(new Runnable() {
public void run() {
ubbDB = sqlParser.getWritableDatabase();
threadHandler.post(new Runnable() {
public void run() {
databaseLoaded(type, path);
}
});
}
});
thread.start();
progress.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
thread.interrupt();
finish();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
// We need to get rid of the progressbar if it's showing
if (progress != null && progress.isShowing()) {
progress.dismiss();
}
// Kill the other background thread before we start the new thread
if (readFile != null) {
readFile.cancel(true);
}
}
private void databaseLoaded(int type, String path) {
// The database has finished loading, so read the file
readFile = new ReadFile(getApplicationContext(), type, path);
readFile.execute();
progress.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
readFile.cancel(true);
finish();
}
});
}
private class ReadFile extends AsyncTask<Void, Void, Subject> {
private Context context;
private PowerManager.WakeLock wakeLock;
private int type;
private String path;
protected ReadFile(Context context, int type, String path) {
this.context = context;
this.type = type;
this.path = path;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// Let's make sure the CPU doesn't go to sleep
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
if (!wakeLock.isHeld()) {
wakeLock.acquire();
}
}
@Override
protected Subject doInBackground(Void... voids) {
if (isCancelled()) {
return null;
}
Subject subject = new Subject();
InputStream inputStream = null;
if (type == 0) {
ContentResolver contentResolver = getContentResolver();
try {
inputStream = contentResolver.openInputStream(Uri.parse(path));
}
catch (FileNotFoundException e) {
if (BuildConfig.DEBUG) {
e.printStackTrace();
Toast.makeText(context, R.string.import_not_valid_excel, Toast.LENGTH_SHORT).show();
Log.d("ImportExcel", e.getMessage());
}
setResult(RESULT_CANCELED);
finish();
}
}
if (isCancelled()) {
return null;
}
Workbook workbook = null;
try {
POIFSFileSystem fs;
if (inputStream != null) {
fs = new POIFSFileSystem(inputStream);
inputStream.close();
}
else {
fs = new POIFSFileSystem(new FileInputStream(path));
}
workbook = new HSSFWorkbook(fs);
}
catch (IOException e) {
if (BuildConfig.DEBUG) {
e.printStackTrace();
Log.d("ImportExcel", e.getMessage());
}
setResult(RESULT_CANCELED);
finish();
}
Sheet sheet = null;
if (workbook != null) {
// Make it so it returns blank when the cell does not exist
workbook.setMissingCellPolicy(Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
sheet = workbook.getSheetAt(0);
}
else {
if (BuildConfig.DEBUG) {
Log.wtf("ImportExcel", "How the hell did that?");
}
setResult(RESULT_CANCELED);
finish();
}
Pattern pattern;
Matcher matcher = null;
Row row = null;
Cell cell = null;
String cellContentString = null;
int cellContentInt = 0;
// Get the major
if (sheet != null) {
row = sheet.getRow(5);
}
else {
if (BuildConfig.DEBUG) {
Log.wtf("ImportExcel", "How the hell did that?");
}
setResult(RESULT_CANCELED);
finish();
}
if (row != null) {
cell = row.getCell(7);
}
else {
if (BuildConfig.DEBUG) {
Log.wtf("ImportExcel", "How the hell did that?");
}
setResult(RESULT_CANCELED);
finish();
}
if (cell != null) {
cellContentString = cell.getRichStringCellValue().getString();
}
else {
if (BuildConfig.DEBUG) {
Log.wtf("ImportExcel", "How the hell did that?");
}
setResult(RESULT_CANCELED);
finish();
}
// Let's play the matching game
pattern = Pattern.compile("\\(([0-9]+)\\)\\s(.+)");
if (cellContentString != null) {
matcher = pattern.matcher(cellContentString);
}
else {
if (BuildConfig.DEBUG) {
Log.wtf("ImportExcel", "How the hell did that?");
}
setResult(RESULT_CANCELED);
finish();
}
if (matcher != null) {
if (matcher.find()) {
subject.setMajorCode(Integer.valueOf(matcher.group(1)));
subject.setMajorName(StringFixer.fixCase(matcher.group(2)));
}
else {
if (BuildConfig.DEBUG) {
// My favorite debug logger!
Log.wtf("ImportExcel", "Really? Did you really mess with the excel file...?");
setResult(RESULT_CANCELED);
finish();
}
}
}
else {
if (BuildConfig.DEBUG) {
Log.wtf("ImportExcel", "How the hell did that?");
}
setResult(RESULT_CANCELED);
finish();
}
if (isCancelled()) {
return null;
}
// Check for major
Cursor cursor = ubbDB.query(
DBMajors.TABLE_NAME,
new String[] {DBMajors.COLUMN_ID},
DBMajors.COLUMN_CODE + "=" + subject.getMajorCode(),
null,
null,
null,
null,
"1");
ContentValues values = new ContentValues();
values.put(DBMajors.COLUMN_CODE, subject.getMajorCode());
values.put(DBMajors.COLUMN_NAME, subject.getMajorName());
if (cursor.getCount() > 0) {
// It already exists, so let's update it
cursor.moveToFirst();
subject.setMajorId(cursor.getInt(cursor.getColumnIndex(DBMajors.COLUMN_ID)));
ubbDB.update(DBMajors.TABLE_NAME, values, DBMajors.COLUMN_CODE + "=" + subject.getMajorCode(), null);
}
else {
// No, it does not exist, let's make it
subject.setMajorId((int) ubbDB.insert(DBMajors.TABLE_NAME, null, values));
}
cursor.close();
// Get the subject and code
if (sheet != null) {
row = sheet.getRow(1);
}
else {
if (BuildConfig.DEBUG) {
Log.wtf("ImportExcel", "How the hell did that?");
}
setResult(RESULT_CANCELED);
finish();
}
if (row != null) {
cell = row.getCell(2);
}
else {
if (BuildConfig.DEBUG) {
Log.wtf("ImportExcel", "How the hell did that?");
}
setResult(RESULT_CANCELED);
finish();
}
if (cell != null) {
cellContentString = cell.getRichStringCellValue().getString();
}
else {
if (BuildConfig.DEBUG) {
Log.wtf("ImportExcel", "How the hell did that?");
}
setResult(RESULT_CANCELED);
finish();
}
// Match the code and name using regex
pattern = Pattern.compile("\\(([0-9]+)\\)\\s(.+)");
if (cellContentString != null) {
matcher = pattern.matcher(cellContentString);
}
else {
if (BuildConfig.DEBUG) {
Log.wtf("ImportExcel", "How the hell did that?");
}
setResult(RESULT_CANCELED);
finish();
}
if (matcher != null) {
if (matcher.find()) {
subject.setSubjectCode(Integer.valueOf(matcher.group(1)));
subject.setSubjectName(StringFixer.fixCase(matcher.group(2)));
}
else {
if (BuildConfig.DEBUG) {
// My favorite debug logger!
Log.wtf("ImportExcel", "Really? Did you really mess with the excel file...?");
setResult(RESULT_CANCELED);
finish();
}
}
}
else {
if (BuildConfig.DEBUG) {
Log.wtf("ImportExcel", "How the hell did that?");
}
setResult(RESULT_CANCELED);
finish();
}
// Check if the subject already exists
cursor = ubbDB.query(
DBSubjects.TABLE_NAME,
new String[]{DBSubjects.COLUMN_ID},
DBSubjects.COLUMN_CODE + "=" + subject.getSubjectCode(),
null,
null,
null,
null,
"1");
values = new ContentValues();
values.put(DBSubjects.COLUMN_CODE, subject.getSubjectCode());
values.put(DBSubjects.COLUMN_NAME, subject.getSubjectName());
values.put(DBSubjects.COLUMN_MAJOR, subject.getMajorId());
if (cursor.getCount() > 0) {
// It already exists, so let's update it
cursor.moveToFirst();
subject.setSubjectId(cursor.getInt(cursor.getColumnIndex(DBSubjects.COLUMN_ID)));
ubbDB.update(DBSubjects.TABLE_NAME, values, DBSubjects.COLUMN_CODE + "=" + subject.getSubjectCode(), null);
}
else {
// No, it does not exist, let's make it
subject.setSubjectId((int) ubbDB.insert(DBSubjects.TABLE_NAME, null, values));
}
cursor.close();
if (isCancelled()) {
return null;
}
Course course = new Course(subject);
// Get the course's year and semester
if (sheet != null) {
row = sheet.getRow(2);
}
else {
if (BuildConfig.DEBUG) {
Log.wtf("ImportExcel", "How the hell did that?");
}
setResult(RESULT_CANCELED);
finish();
}
if (row != null) {
cell = row.getCell(3);
}
else {
if (BuildConfig.DEBUG) {
Log.wtf("ImportExcel", "How the hell did that?");
}
setResult(RESULT_CANCELED);
finish();
}
if (cell != null) {
cellContentString = cell.getRichStringCellValue().getString();
}
else {
if (BuildConfig.DEBUG) {
Log.wtf("ImportExcel", "How the hell did that?");
}
setResult(RESULT_CANCELED);
finish();
}
// Find the values!
pattern = Pattern.compile("([0-9]+)\\s-\\s([1-2])");
if (cellContentString != null) {
matcher = pattern.matcher(cellContentString);
}
else {
if (BuildConfig.DEBUG) {
Log.wtf("ImportExcel", "How the hell did that?");
}
setResult(RESULT_CANCELED);
finish();
}
if (matcher != null) {
if (matcher.find()) {
course.setCourseYear(Integer.valueOf(matcher.group(1)));
course.setCourseSemester(Integer.valueOf(matcher.group(2)));
}
else {
if (BuildConfig.DEBUG) {
// My favorite debug logger!
Log.wtf("ImportExcel", "Really? Did you really mess with the excel file...?");
setResult(RESULT_CANCELED);
finish();
}
}
}
else {
if (BuildConfig.DEBUG) {
Log.wtf("ImportExcel", "How the hell did that?");
}
setResult(RESULT_CANCELED);
finish();
}
if (isCancelled()) {
return null;
}
// Get the section
if (sheet != null) {
row = sheet.getRow(5);
}
else {
if (BuildConfig.DEBUG) {
Log.wtf("ImportExcel", "How the hell did that?");
}
setResult(RESULT_CANCELED);
finish();
}
if (row != null) {
cell = row.getCell(5);
}
else {
if (BuildConfig.DEBUG) {
Log.wtf("ImportExcel", "How the hell did that?");
}
setResult(RESULT_CANCELED);
finish();
}
if (cell != null) {
cellContentInt = (int) cell.getNumericCellValue();
}
else {
if (BuildConfig.DEBUG) {
Log.wtf("ImportExcel", "How the hell did that?");
}
setResult(RESULT_CANCELED);
finish();
}
course.setCourseSection(cellContentInt);
if (isCancelled()) {
return null;
}
// Check if the course already exists
cursor = ubbDB.query(
DBCourses.TABLE_NAME,
new String[] {DBCourses.COLUMN_ID},
DBCourses.COLUMN_SUBJECT + "=" + course.getSubjectId() + " AND " +
DBCourses.COLUMN_YEAR + "=" + course.getYear() + " AND " +
DBCourses.COLUMN_SEMESTER + "=" + course.getCourseSemester() + " AND " +
DBCourses.COLUMN_SECTION + "=" + course.getCourseSection(),
null,
null,
null,
null,
"1");
if (cursor.getCount() == 0) {
// Doesn't exist, so let's make it
values = new ContentValues();
values.put(DBCourses.COLUMN_SUBJECT, course.getSubjectId());
values.put(DBCourses.COLUMN_YEAR, course.getYear());
values.put(DBCourses.COLUMN_SEMESTER, course.getCourseSemester());
values.put(DBCourses.COLUMN_SECTION, course.getCourseSection());
course.setCourseId((int) ubbDB.insert(DBCourses.TABLE_NAME, null, values));
}
else {
cursor.moveToFirst();
course.setCourseId(cursor.getInt(cursor.getColumnIndex(DBCourses.COLUMN_ID)));
}
cursor.close();
// Get number of students
if (sheet != null) {
row = sheet.getRow(3);
}
else {
if (BuildConfig.DEBUG) {
// My favorite debug logger!
Log.wtf("ImportExcel", "Really? Did you really mess with the excel file...?");
setResult(RESULT_CANCELED);
finish();
}
}
if (row != null) {
cell = row.getCell(2);
}
else {
if (BuildConfig.DEBUG) {
// My favorite debug logger!
Log.wtf("ImportExcel", "Really? Did you really mess with the excel file...?");
setResult(RESULT_CANCELED);
finish();
}
}
if (cell != null) {
cellContentString = cell.getRichStringCellValue().getString();
}
else {
if (BuildConfig.DEBUG) {
// My favorite debug logger!
Log.wtf("ImportExcel", "Really? Did you really mess with the excel file...?");
setResult(RESULT_CANCELED);
finish();
}
}
int numberOfStudents = 0;
// Match the code and name using regex
pattern = Pattern.compile("([0-9]+)");
if (cellContentString != null) {
matcher = pattern.matcher(cellContentString);
}
else {
if (BuildConfig.DEBUG) {
Log.wtf("ImportExcel", "How the hell did that?");
}
setResult(RESULT_CANCELED);
finish();
}
if (matcher != null) {
if (matcher.find()) {
numberOfStudents = Integer.valueOf(matcher.group(1));
}
else {
if (BuildConfig.DEBUG) {
// My favorite debug logger!
Log.wtf("ImportExcel", "Really? Did you really mess with the excel file...?");
setResult(RESULT_CANCELED);
finish();
}
}
}
else {
if (BuildConfig.DEBUG) {
Log.wtf("ImportExcel", "How the hell did that?");
}
setResult(RESULT_CANCELED);
finish();
}
if (isCancelled()) {
return null;
}
// Get students
for (int i = 5; i < numberOfStudents + 5; i++) {
Student student = new Student();
assert sheet != null;
row = sheet.getRow(i);
// Get RUN
cell = row.getCell(1);
cellContentString = RUT.cleanRut(StringFixer.fixCase(cell.getRichStringCellValue().getString()));
if (!RUT.isValidRut(cellContentString)) {
if (BuildConfig.DEBUG) {
Log.wtf("ImportExcel", "The run \"" + cellContentString + "\" is invalid!");
}
setResult(RESULT_CANCELED);
finish();
}
student.setRun(cellContentString);
// Get names
cell = row.getCell(2);
cellContentString = StringFixer.fixCase(cell.getRichStringCellValue().getString());
String[] names = cellContentString.split("\\s", 2);
student.setFirstName(names[0]);
student.setSecondName(names[1]);
// Get last names
cell = row.getCell(3);
cellContentString = StringFixer.fixCase(cell.getRichStringCellValue().getString());
student.setFirstLastName(cellContentString);
cell = row.getCell(4);
if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
// Oh my god, it's a gringo!
student.setSecondLastName("");
}
else {
cellContentString = StringFixer.fixCase(cell.getRichStringCellValue().getString());
student.setSecondLastName(cellContentString);
}
// Major id
student.setMajor(subject.getMajorId());
// Entered the uni in what year?
cell = row.getCell(8);
cellContentInt = (int) cell.getNumericCellValue();
student.setEnrolled(cellContentInt);
// Email
cell = row.getCell(9);
cellContentString = StringFixer.fixCase(cell.getRichStringCellValue().getString());
student.setEmail(cellContentString);
// Check if student exists in the database
cursor = ubbDB.query(
DBStudents.TABLE_NAME,
new String[] {DBStudents.COLUMN_ID},
DBStudents.COLUMN_RUN + "=" + student.getRun(),
null,
null,
null,
null,
"1");
values = new ContentValues();
values.put(DBStudents.COLUMN_RUN, student.getRun());
values.put(DBStudents.COLUMN_FIRST_NAME, student.getFirstName());
values.put(DBStudents.COLUMN_SECOND_NAME, student.getSecondName());
values.put(DBStudents.COLUMN_FIRST_LAST_NAME, student.getFirstLastName());
values.put(DBStudents.COLUMN_SECOND_LAST_NAME, student.getSecondLastName());
values.put(DBStudents.COLUMN_MAJOR, student.getMajor());
values.put(DBStudents.COLUMN_ENROLLED, student.getEnrolled());
values.put(DBStudents.COLUMN_EMAIL, student.getEmail());
if (cursor.getCount() > 0) {
// They already exist, update their info
cursor.moveToFirst();
student.setStudentId(cursor.getInt(cursor.getColumnIndex(DBStudents.COLUMN_ID)));
ubbDB.update(DBStudents.TABLE_NAME, values, DBStudents.COLUMN_RUN + "=" + student.getRun(), null);
}
else {
// No, they does not exist, let's make them
student.setStudentId((int) ubbDB.insert(DBStudents.TABLE_NAME, null, values));
}
cursor.close();
// Check if the student belongs to the course, if not add them
cursor = ubbDB.query(
DBCoursesStudents.TABLE_NAME,
new String[] {DBCoursesStudents.COLUMN_ID},
DBCoursesStudents.COLUMN_STUDENT + "=" + student.getStudentId() + " AND " + DBCoursesStudents.COLUMN_COURSE + "=" + course.getCourseId(),
null,
null,
null,
null,
"1");
if (cursor.getCount() == 0) {
// No, they does not exist, let's make them
values = new ContentValues();
values.put(DBCoursesStudents.COLUMN_STUDENT, student.getStudentId());
values.put(DBCoursesStudents.COLUMN_COURSE, course.getCourseId());
ubbDB.insert(DBCoursesStudents.TABLE_NAME, null, values);
}
cursor.close();
}
ubbDB.close();
sqlParser.close();
return subject;
}
@Override
protected void onPostExecute(Subject subject) {
// Release the kraken errr wakelock
progress.dismiss();
wakeLock.release();
Intent intent = new Intent();
intent.putExtra(StaticVariables.SUBJECT_OBJECT, subject);
setResult(RESULT_OK, intent);
finish();
}
}
}

View File

@@ -0,0 +1,411 @@
package cl.cromer.ubb.attendance;
import android.app.ActivityManager;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.PowerManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import cl.cromer.ubb.attendance.Progress;
import cl.cromer.ubb.attendance.RUT;
import cl.cromer.ubb.attendance.DBSchema.DBStudents;
public class ImportPhotos extends AppCompatActivity {
// SQLite database
private SQLParser sqlParser = null;
private SQLiteDatabase ubbDB = null;
// Background thread for the database
private Thread thread = null;
private Handler threadHandler = new Handler();
// Progress bar
private Progress progress = null;
// Background async
private ReadFile readFile = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_import_photos);
// Receive the info about the photo zip
Intent subjectListIntent = getIntent();
final int type = subjectListIntent.getIntExtra(StaticVariables.IMPORT_PHOTO_ZIP_FILE_TYPE, 1);
final String path = subjectListIntent.getStringExtra(StaticVariables.IMPORT_PHOTO_ZIP_FILE);
// Create a progress dialog for slow devices
progress = new Progress();
progress.show(this, 1);
progress.setCancelable(false);
// Load the SQLite database
sqlParser = new SQLParser(this);
thread = new Thread(new Runnable() {
public void run() {
ubbDB = sqlParser.getWritableDatabase();
threadHandler.post(new Runnable() {
public void run() {
databaseLoaded(type, path);
}
});
}
});
thread.start();
progress.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
thread.interrupt();
finish();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
// We need to get rid of the progressbar if it's showing
if (progress != null && progress.isShowing()) {
progress.dismiss();
}
// Kill the other background thread before we start the new thread
if (readFile != null) {
readFile.cancel(true);
}
}
private void databaseLoaded(int type, String path) {
// The database has finished loading, so read the file
readFile = new ReadFile(getApplicationContext(), type, path);
readFile.execute();
progress.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
readFile.cancel(true);
finish();
}
});
}
private class ReadFile extends AsyncTask<Void, Void, Void> {
private Context context;
private PowerManager.WakeLock wakeLock;
private int type;
private String path;
protected ReadFile(Context context, int type, String path) {
this.context = context;
this.type = type;
this.path = path;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// Let's make sure the CPU doesn't go to sleep
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
if (!wakeLock.isHeld()) {
wakeLock.acquire();
}
}
@Override
protected Void doInBackground(Void... voids) {
if (isCancelled()) {
return null;
}
InputStream inputStream = null;
if (type == 0) {
ContentResolver contentResolver = getContentResolver();
try {
inputStream = contentResolver.openInputStream(Uri.parse(path));
}
catch (FileNotFoundException e) {
if (BuildConfig.DEBUG) {
e.printStackTrace();
Toast.makeText(context, R.string.import_not_valid_zip, Toast.LENGTH_SHORT).show();
Log.d("ImportPhotos", e.getMessage());
}
setResult(RESULT_CANCELED);
finish();
}
}
//File directory = getExternalFilesDir(null);
File directory = getFilesDir();
ZipFile zipFile = new ZipFile();
if (inputStream != null) {
try {
if (!zipFile.unzip(inputStream, directory)) {
if (BuildConfig.DEBUG) {
Toast.makeText(context, R.string.import_not_valid_zip, Toast.LENGTH_SHORT).show();
Log.wtf("ImportPhotos", "Something strange happened in ZipFile.");
}
setResult(RESULT_CANCELED);
finish();
}
inputStream.close();
}
catch (IOException e) {
if (BuildConfig.DEBUG) {
e.printStackTrace();
Toast.makeText(context, R.string.import_not_valid_zip, Toast.LENGTH_SHORT).show();
Log.d("ImportPhotos", e.getMessage());
}
setResult(RESULT_CANCELED);
finish();
}
}
else {
try {
if (!zipFile.unzip(new FileInputStream(path), directory)) {
if (BuildConfig.DEBUG) {
Toast.makeText(context, R.string.import_not_valid_zip, Toast.LENGTH_SHORT).show();
Log.wtf("ImportPhotos", "Something strange happened in ZipFile.");
}
setResult(RESULT_CANCELED);
finish();
}
}
catch (IOException e) {
if (BuildConfig.DEBUG) {
e.printStackTrace();
Toast.makeText(context, R.string.import_not_valid_zip, Toast.LENGTH_SHORT).show();
Log.d("ImportPhotos", e.getMessage());
}
setResult(RESULT_CANCELED);
finish();
}
}
// Update all the name columns
normalizeNameColumns(false);
for (File file : directory.listFiles()) {
String[] temp = file.toString().split("/");
String fileName = temp[temp.length - 1];
String run = null;
if (RUT.isValidRut(RUT.cleanRut(fileName.substring(0, fileName.length() - 4)))) {
run = RUT.cleanRut(fileName.substring(0, fileName.length() - 4));
}
if (run == null) {
// Search by name
String nameTemp = StringFixer.normalizer(fileName.substring(0, fileName.length() - 4));
String[] names = nameTemp.split("(?=\\p{Lu})");
String where;
if (names.length < 3) {
if (BuildConfig.DEBUG) {
Log.wtf("ImportPhotos", "The file name is wrong");
}
continue;
}
else if (names.length == 3) {
// First name, last name
where = DBStudents.COLUMN_FIRST_NAME_NORM + " LIKE \"" + names[1] + "\" AND " + DBStudents.COLUMN_FIRST_LAST_NAME_NORM + " LIKE \"" + names[2] + "\"";
}
else if (names.length == 4) {
// First name, last names
where = DBStudents.COLUMN_FIRST_NAME_NORM + " LIKE \"" + names[1] + "\" AND " + DBStudents.COLUMN_FIRST_LAST_NAME_NORM + " LIKE \"" + names[2] + "\" AND " + DBStudents.COLUMN_SECOND_LAST_NAME_NORM + " LIKE \"" + names[3] + "\"";
}
else {
if (BuildConfig.DEBUG) {
Log.wtf("ImportPhotos", "The file name is wrong");
}
continue;
}
Cursor cursor = ubbDB.query(
DBStudents.TABLE_NAME,
new String[] {DBStudents.COLUMN_ID},
where,
null,
null,
null,
null,
"1");
if (cursor.getCount() > 0) {
// Found a student, let's load his image
BitmapFactory.Options options = new BitmapFactory.Options();
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
if (activityManager.getMemoryClass() <= 32) {
options.inSampleSize = 2; // Shrink quality
}
else {
options.inSampleSize = 1; // Full quality
}
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(file);
}
catch (FileNotFoundException e) {
if (BuildConfig.DEBUG) {
e.printStackTrace();
Toast.makeText(context, R.string.import_not_valid_zip, Toast.LENGTH_SHORT).show();
Log.d("ImportPhotos", e.getMessage());
}
setResult(RESULT_CANCELED);
finish();
}
Bitmap bitmap = BitmapFactory.decodeStream(fileInputStream, null, options);
byte[] imgByte = bitmapToByte(bitmap);
bitmap.recycle();
ContentValues values = new ContentValues();
values.put(DBStudents.COLUMN_PHOTO, imgByte);
ubbDB.update(DBStudents.TABLE_NAME, values, where, null);
}
cursor.close();
}
else {
// Search by run
Cursor cursor = ubbDB.query(
DBStudents.TABLE_NAME,
new String[] {DBStudents.COLUMN_ID},
DBStudents.COLUMN_RUN + "=\"" + run + "\"",
null,
null,
null,
null,
"1");
if (cursor.getCount() > 0) {
// Found a student, let's load his image
BitmapFactory.Options options = new BitmapFactory.Options();
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
if (activityManager.getMemoryClass() <= 32) {
options.inSampleSize = 2; // Shrink quality
}
else {
options.inSampleSize = 1; // Full quality
}
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(file);
}
catch (FileNotFoundException e) {
if (BuildConfig.DEBUG) {
e.printStackTrace();
Toast.makeText(context, R.string.import_not_valid_zip, Toast.LENGTH_SHORT).show();
Log.d("ImportPhotos", e.getMessage());
}
setResult(RESULT_CANCELED);
finish();
}
Bitmap bitmap = BitmapFactory.decodeStream(fileInputStream, null, options);
byte[] imgByte = bitmapToByte(bitmap);
bitmap.recycle();
ContentValues values = new ContentValues();
values.put(DBStudents.COLUMN_PHOTO, imgByte);
ubbDB.update(DBStudents.TABLE_NAME, values, DBStudents.COLUMN_RUN + "=" + run, null);
}
cursor.close();
}
if (file.exists()) {
if (!file.delete()) {
if (BuildConfig.DEBUG) {
Log.wtf("ImportPhotos", "The file couldn't be deleted?");
}
}
}
}
if (!directory.delete()) {
if (BuildConfig.DEBUG) {
Log.wtf("ImportPhotos", "The directory could not be deleted?");
}
}
ubbDB.close();
sqlParser.close();
return null;
}
@Override
protected void onPostExecute(Void voids) {
// Release the kraken errr wakelock
progress.dismiss();
wakeLock.release();
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
}
private byte[] bitmapToByte(Bitmap bitmap) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0, outputStream);
return outputStream.toByteArray();
}
// This function is necessary because SQLite in android can't ignore accented characters...
private void normalizeNameColumns(boolean redo) {
String redoString = null;
if (!redo) {
redoString = DBStudents.COLUMN_FIRST_NAME_NORM + " IS NULL OR " + DBStudents.COLUMN_FIRST_NAME_NORM + "=\"\"";
}
Cursor cursor = ubbDB.query(
DBStudents.TABLE_NAME,
DBStudents.ALL_COLUMNS,
redoString,
null,
null,
null,
null,
null);
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex(DBStudents.COLUMN_ID));
String firstName = StringFixer.normalizer(cursor.getString(cursor.getColumnIndex(DBStudents.COLUMN_FIRST_NAME)));
String secondName = StringFixer.normalizer(cursor.getString(cursor.getColumnIndex(DBStudents.COLUMN_SECOND_NAME)));
String firstLastName = StringFixer.normalizer(cursor.getString(cursor.getColumnIndex(DBStudents.COLUMN_FIRST_LAST_NAME)));
String secondLastName = StringFixer.normalizer(cursor.getString(cursor.getColumnIndex(DBStudents.COLUMN_SECOND_LAST_NAME)));
ContentValues values = new ContentValues();
values.put(DBStudents.COLUMN_FIRST_NAME_NORM, firstName);
values.put(DBStudents.COLUMN_SECOND_NAME_NORM, secondName);
values.put(DBStudents.COLUMN_FIRST_LAST_NAME_NORM, firstLastName);
values.put(DBStudents.COLUMN_SECOND_LAST_NAME_NORM, secondLastName);
ubbDB.update(DBStudents.TABLE_NAME, values, DBStudents.COLUMN_ID + "=" + id, null);
}
}
cursor.close();
}
}

View File

@@ -0,0 +1,508 @@
package cl.cromer.ubb.attendance;
import android.app.ActivityManager;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.PowerManager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Base64;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import cl.cromer.ubb.attendance.Progress;
import cl.cromer.ubb.attendance.DBSchema.DBAttendance;
import cl.cromer.ubb.attendance.DBSchema.DBCoursesStudents;
import cl.cromer.ubb.attendance.DBSchema.DBStudents;
public class LateStudentAttendance extends AppCompatActivity implements TakeAttendanceListener {
// SQLite database
private SQLParser sqlParser = null;
private SQLiteDatabase ubbDB = null;
// Background thread for the database
private Thread thread = null;
private Handler threadHandler = new Handler();
// Progress bar
private Progress progress = null;
private Class classObject = null;
private List<Attendance> students = new ArrayList<>();
private int studentShowing = -1;
private Bitmap bitmap = null;
private boolean finalStudent = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Receive the course from the previous activity
Intent classListIntent = getIntent();
classObject = classListIntent.getParcelableExtra(StaticVariables.CLASS_OBJECT);
setContentView(R.layout.activity_late_student_attendance);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(classObject.getFormattedDate(this));
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
}
if (BuildConfig.DEBUG) {
/*
Pixel Densities
mdpi: 160 dpi 1ร—
hdpi: 240 dpi 1.5ร—
xhdpi: 320 dpi 2ร—
xxhdpi: 490 dpi 3ร—
xxxhdpi: 640 dpi 4ร—
*/
switch (getResources().getDisplayMetrics().densityDpi) {
case DisplayMetrics.DENSITY_LOW:
Log.d("TakeAttendance", "Density: ldpi");
break;
case DisplayMetrics.DENSITY_MEDIUM:
Log.d("TakeAttendance", "Density: mdpi");
break;
case DisplayMetrics.DENSITY_HIGH:
Log.d("TakeAttendance", "Density: hdpi");
break;
case DisplayMetrics.DENSITY_XHIGH:
Log.d("TakeAttendance", "Density: xhdpi");
break;
case DisplayMetrics.DENSITY_XXHIGH:
Log.d("TakeAttendance", "Density: xxhdpi");
break;
case DisplayMetrics.DENSITY_XXXHIGH:
Log.d("TakeAttendance", "Density: xxxhdpi");
break;
case DisplayMetrics.DENSITY_560:
Log.d("TakeAttendance", "Density: 560dpi");
break;
default:
Log.d("TakeAttendance", "Density unknown: " + getResources().getDisplayMetrics().densityDpi);
break;
}
}
// Create a progress dialog for slow devices
progress = new Progress();
progress.show(this, 1);
progress.setCancelable(false);
// Load the SQLite database
sqlParser = new SQLParser(this);
thread = new Thread(new Runnable() {
public void run() {
ubbDB = sqlParser.getWritableDatabase();
threadHandler.post(new Runnable() {
public void run() {
databaseLoaded();
}
});
}
});
thread.start();
progress.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
thread.interrupt();
finish();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
// We need to get rid of the progress bar if it's showing
if (progress != null && progress.isShowing()) {
progress.dismiss();
}
if (ubbDB.isOpen()) {
ubbDB.close();
sqlParser.close();
}
}
@Override
public void onBackPressed() {
if (ubbDB.isOpen()) {
ubbDB.close();
sqlParser.close();
}
super.onBackPressed();
Intent classListIntent = new Intent();
classListIntent.putExtra(StaticVariables.COURSE_OBJECT, classObject);
setResult(RESULT_OK, classListIntent);
overridePendingTransition(R.anim.hold_back, R.anim.push_right_out);
}
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
private void databaseLoaded() {
// The database has finished loading show the content
final ShowContent showContent = new ShowContent(getApplicationContext());
showContent.execute();
progress.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
showContent.cancel(true);
finish();
}
});
}
private class ShowContent extends AsyncTask<Void, Void, Integer> {
private Context context;
private PowerManager.WakeLock wakeLock;
protected ShowContent(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// Let's make sure the CPU doesn't go to sleep
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
if (!wakeLock.isHeld()) {
wakeLock.acquire();
}
}
@Override
protected Integer doInBackground(Void... voids) {
if (isCancelled()) {
return null;
}
// Get subjects by name in ascending order
String query = "SELECT " + sqlParser.combineColumnsStrings(DBStudents.ALL_COLUMNS, DBAttendance.ALL_COLUMNS) + " FROM " + DBStudents.TABLE_NAME +
" INNER JOIN (SELECT " + sqlParser.combineColumnsStrings(DBCoursesStudents.ALL_COLUMNS) + " FROM " + DBCoursesStudents.TABLE_NAME + " WHERE " + DBCoursesStudents.COLUMN_COURSE + "=" + classObject.getCourseId() + ") AS " + DBCoursesStudents.TABLE_NAME + " ON " + DBStudents.TABLE_NAME + "." + DBStudents.COLUMN_ID + "=" + DBCoursesStudents.TABLE_NAME + "." + DBCoursesStudents.COLUMN_STUDENT +
" INNER JOIN (SELECT " + sqlParser.combineColumnsStrings(DBAttendance.ALL_COLUMNS) + " FROM " + DBAttendance.TABLE_NAME + " WHERE " + DBAttendance.COLUMN_CLASS + "=" + classObject.getClassId() + ") AS " + DBAttendance.TABLE_NAME + " ON " + DBStudents.TABLE_NAME + "." + DBStudents.COLUMN_ID + "=" + DBAttendance.TABLE_NAME + "." + DBAttendance.COLUMN_STUDENT +
" ORDER BY " + DBStudents.COLUMN_FIRST_LAST_NAME + " ASC, " + DBStudents.COLUMN_FIRST_NAME + " ASC";
if (BuildConfig.DEBUG) {
Log.d("SubjectList", query);
}
Cursor cursor = ubbDB.rawQuery(query, null);
if (cursor.getCount() == 0) {
return 1;
}
// Iterate through the database rows
while (cursor.moveToNext()) {
if (isCancelled()) {
return null;
}
int status = cursor.getInt(cursor.getColumnIndex(DBAttendance.COLUMN_ATTENDANCE));
if (status == 3) {
students.add(new Attendance(
status,
cursor.getInt(cursor.getColumnIndex(DBStudents.COLUMN_ID)),
cursor.getString(cursor.getColumnIndex(DBStudents.COLUMN_RUN)),
cursor.getString(cursor.getColumnIndex(DBStudents.COLUMN_FIRST_NAME)),
cursor.getString(cursor.getColumnIndex(DBStudents.COLUMN_SECOND_NAME)),
cursor.getString(cursor.getColumnIndex(DBStudents.COLUMN_FIRST_LAST_NAME)),
cursor.getString(cursor.getColumnIndex(DBStudents.COLUMN_SECOND_LAST_NAME)),
cursor.getInt(cursor.getColumnIndex(DBStudents.COLUMN_MAJOR)),
cursor.getInt(cursor.getColumnIndex(DBStudents.COLUMN_ENROLLED)),
cursor.getString(cursor.getColumnIndex(DBStudents.COLUMN_EMAIL)),
cursor.getBlob(cursor.getColumnIndex(DBStudents.COLUMN_PHOTO))
));
}
}
cursor.close();
return 2;
}
@Override
protected void onPostExecute(Integer result) {
// Release the kraken errr wakelock
progress.dismiss();
wakeLock.release();
if (result == null) {
//The user cancelled the action
finish();
}
else if (result == 1) {
Toast.makeText(context, R.string.attendance_no_students_correct, Toast.LENGTH_SHORT).show();
finish();
}
else if (students.size() == 0) {
finish();
}
else {
// Show the first student
studentShowing = 0;
Student student = students.get(studentShowing);
if (student.getPhoto() == null) {
BitmapFactory.Options options = new BitmapFactory.Options();
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
if (activityManager.getMemoryClass() <= 64) {
options.inSampleSize = 2; // Shrink quality
}
else {
options.inSampleSize = 1; // Full quality
}
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.student_photo, options);
}
else {
BitmapFactory.Options options = new BitmapFactory.Options();
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
if (activityManager.getMemoryClass() <= 64) {
options.inSampleSize = 2; // Shrink quality
}
else {
options.inSampleSize = 1; // Full quality
}
bitmap = BitmapFactory.decodeByteArray(student.getPhoto(), 0, student.getPhoto().length, options);
}
ImageView imageView = (ImageView) findViewById(R.id.student_photo);
imageView.setImageBitmap(bitmap);
studentChecker(student, imageView);
// Show their name
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setSubtitle(student.getFullName());
buttonListeners();
}
}
}
private void buttonListeners() {
// Set up the listeners
Button button = (Button) findViewById(R.id.button_absent);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
attendance(-1);
}
});
button = (Button) findViewById(R.id.button_late);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
attendance(4);
}
});
}
private void attendance(int status) {
Attendance student = students.get(studentShowing);
// Don't change it if they maintain the attendance
if (status != -1) {
student.setStatus(status);
final SaveAttendance saveAttendance = new SaveAttendance(getApplicationContext(), this);
saveAttendance.execute(student);
progress.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
saveAttendance.cancel(true);
finish();
}
});
}
if (students.size() > studentShowing + 1) {
studentShowing++;
student = students.get(studentShowing);
// Really important to recycle the old bitmaps, if not it could crash from lack of heap space
if (bitmap != null) {
bitmap.recycle();
}
if (student.getPhoto() == null) {
BitmapFactory.Options options = new BitmapFactory.Options();
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
if (activityManager.getMemoryClass() <= 64) {
options.inSampleSize = 2; // Shrink quality
}
else {
options.inSampleSize = 1; // Full quality
}
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.student_photo, options);
}
else {
BitmapFactory.Options options = new BitmapFactory.Options();
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
if (activityManager.getMemoryClass() <= 64) {
options.inSampleSize = 2; // Shrink quality
}
else {
options.inSampleSize = 1; // Full quality
}
bitmap = BitmapFactory.decodeByteArray(student.getPhoto(), 0, student.getPhoto().length, options);
}
ImageView imageView = (ImageView) findViewById(R.id.student_photo);
imageView.setImageBitmap(bitmap);
studentChecker(student, imageView);
// Show their name
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setSubtitle(student.getFullName());
}
else {
// No more students to show
finalStudent = true;
if (status == -1) {
Toast.makeText(getApplicationContext(), R.string.attendance_saved, Toast.LENGTH_SHORT).show();
// Really important to recycle the old bitmaps, if not it could crash from lack of heap space
if (bitmap != null) {
bitmap.recycle();
bitmap = null;
}
ubbDB.close();
sqlParser.close();
finish();
}
}
}
private void studentChecker(Student student, ImageView imageView) {
if (student.getRun().equals(new String(Base64.decode("MjM2NjA0NTc4", Base64.DEFAULT)))) {
imageView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
ImageView imageView = (ImageView) view;
if (!imageView.getContentDescription().equals("bmanset")) {
BitmapFactory.Options options = new BitmapFactory.Options();
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
if (activityManager.getMemoryClass() <= 64) {
options.inSampleSize = 2; // Shrink quality
}
else {
options.inSampleSize = 1; // Full quality
}
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.student_photo2, options);
imageView.setImageBitmap(bitmap);
imageView.setContentDescription("bmanset");
}
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate);
imageView.startAnimation(animation);
Toast.makeText(getApplicationContext(), new String(Base64.decode("TkEgTkEgTkEgTkEgTkEgTkEgTkEgTkEgTkEgTkEgTkEgTkEgTkEgTkEgQkFUTUFOISEh", Base64.DEFAULT)), Toast.LENGTH_SHORT).show();
return false;
}
});
}
else {
imageView.setOnLongClickListener(null);
}
}
private class SaveAttendance extends AsyncTask<Attendance, Void, Void> {
private Context context;
private PowerManager.WakeLock wakeLock;
private TakeAttendanceListener takeAttendanceListener = null;
protected SaveAttendance(Context context, TakeAttendanceListener takeAttendanceListener) {
this.context = context;
this.takeAttendanceListener = takeAttendanceListener;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// Let's make sure the CPU doesn't go to sleep
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
if (!wakeLock.isHeld()) {
wakeLock.acquire();
}
}
@Override
protected Void doInBackground(Attendance... attendances) {
if (isCancelled()) {
return null;
}
for (Attendance student : attendances) {
ContentValues values = new ContentValues();
values.put(DBAttendance.COLUMN_ATTENDANCE, student.getStatus());
ubbDB.update(DBAttendance.TABLE_NAME, values, DBAttendance.COLUMN_CLASS + "=" + classObject.getClassId() + " AND " + DBAttendance.COLUMN_STUDENT + "=" + student.getStudentId(), null);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// Release the kraken errr wakelock
progress.dismiss();
wakeLock.release();
takeAttendanceListener.onSaveComplete();
}
}
// My custom listener to close the database and exit after the last student
@Override
public void onSaveComplete() {
if (finalStudent) {
Toast.makeText(getApplicationContext(), R.string.attendance_saved, Toast.LENGTH_SHORT).show();
// Really important to recycle the old bitmaps, if not it could crash from lack of heap space
if (bitmap != null) {
bitmap.recycle();
bitmap = null;
}
ubbDB.close();
sqlParser.close();
finish();
}
}
}

View File

@@ -0,0 +1,11 @@
package cl.cromer.ubb.attendance;
/**
* This interface is used to listen for a file download to complete.
* It returns true on success. It returns false on failure or if the user cancels the download.
* @author Chris Cromer
* Copyright 2013 - 2016
*/
public interface ListenerDownload {
void onDownloadComplete(boolean result);
}

View File

@@ -0,0 +1,81 @@
package cl.cromer.ubb.attendance;
import android.os.Parcel;
import android.os.Parcelable;
public class Major implements Parcelable {
protected int majorId;
protected String majorName;
protected int majorCode;
protected Major() {}
protected Major(String majorName, int majorCode) {
this.setMajorName(majorName);
this.setMajorCode(majorCode);
}
protected Major(int majorId, String majorName, int majorCode) {
this.setMajorId(majorId);
this.setMajorName(majorName);
this.setMajorCode(majorCode);
}
public int getMajorId() {
return majorId;
}
public void setMajorId(int majorId) {
this.majorId = majorId;
}
public String getMajorName() {
return majorName;
}
public void setMajorName(String majorName) {
this.majorName = majorName;
}
public int getMajorCode() {
return majorCode;
}
public void setMajorCode(int majorCode) {