Created
June 4, 2020 05:49
-
-
Save abedputra/2cdbae26d582cbcdbdb7f894a4b657bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.attendacewithfingerprintinternal; | |
import android.annotation.SuppressLint; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.database.Cursor; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.location.Location; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.Menu; | |
import android.view.MenuInflater; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.widget.ImageView; | |
import android.widget.LinearLayout; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import com.example.attendacewithfingerprintinternal.database.DBManager; | |
import com.example.attendacewithfingerprintinternal.database.DatabaseHelper; | |
import com.example.attendacewithfingerprintinternal.gps.LocationManagerInterface; | |
import com.example.attendacewithfingerprintinternal.sharedpreferences.PrefKeys; | |
import com.example.attendacewithfingerprintinternal.sharedpreferences.PrefUtils; | |
import java.io.File; | |
// Main activity | |
public class MainActivity extends BaseActivity implements LocationManagerInterface { | |
public DBManager dbManager; | |
double latitude; // Latitude | |
double longitude; // Longitude | |
boolean hasDataStatusCheck; | |
String getStatus, getStatusOvertime; | |
String folderPath = "/attendance_qr/"; | |
String linkDB, keyDB, userNameDB, idNumber, departmentDB, userName; | |
ImageView profilePicMain; | |
TextView nameUserMain; | |
@SuppressLint("SetTextI18n") | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
//open databse | |
dbManager = new DBManager(this); | |
dbManager.open(); | |
// Check if url, key and name already filled in database | |
checkUrlKeyName(); | |
// Check if STATUS_CHECK has data | |
hasDataStatusCheck = PrefUtils.hasKey(this, PrefKeys.STATUS_CHECK); | |
// Get status check in or out | |
getStatus = (String) PrefUtils.getFromPrefs(this, PrefKeys.STATUS_CHECK, ""); | |
Log.d(TAG, "onCreate: " + getStatus); | |
getStatusOvertime = (String) PrefUtils.getFromPrefs(this, PrefKeys.AVAILABLE_OVERTIME, ""); | |
nameUserMain = findViewById(R.id.nameUserMain); | |
nameUserMain.setText("Hi, " + userNameDB + "!"); | |
profilePicMain = findViewById(R.id.profilePicMain); | |
// Click check-in | |
LinearLayout inbutton = findViewById(R.id.inbutton); | |
inbutton.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
// Check if status equals check-out | |
// if (hasDataStatusCheck && getStatus.equals("out_time") && getStatusOvertime.equals("no")) { | |
// can click button | |
Intent intent = new Intent(getApplicationContext(), SendingDataAbsentActivity.class); | |
intent.putExtra("COMMAND_MESSAGE", "in_time"); | |
startActivity(intent); | |
// } else { | |
// // Already check-in | |
// Toast.makeText(MainActivity.this, getString(R.string.already_check_in), Toast.LENGTH_LONG).show(); | |
// } | |
} | |
}); | |
// Click check-out | |
LinearLayout outbutton = findViewById(R.id.outbutton); | |
outbutton.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
// Check if status equals check-out | |
// if (hasDataStatusCheck && getStatus.equals("break_in") || getStatus.equals("in_time") || getStatus.equals("interruptions_in")) { | |
Intent intent = new Intent(getApplicationContext(), SendingDataAbsentActivity.class); | |
intent.putExtra("COMMAND_MESSAGE", "out_time"); | |
startActivity(intent); | |
// } else { | |
// // Already check-in | |
// Toast.makeText(MainActivity.this, "Cannot Check-Out, Please Break-in, or Check-in, or Interruptions-in first.", Toast.LENGTH_LONG).show(); | |
// } | |
} | |
}); | |
// Click check break out | |
LinearLayout break_out = findViewById(R.id.break_out); | |
break_out.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
// if (hasDataStatusCheck && getStatus.equals("in_time") || getStatus.equals("interruptions_in")) { | |
Intent intent = new Intent(getApplicationContext(), SendingDataAbsentActivity.class); | |
intent.putExtra("COMMAND_MESSAGE", "break_out"); | |
startActivity(intent); | |
// } else { | |
// // Already check-in | |
// Toast.makeText(MainActivity.this, "Please Check-In or Interruptions-in first.", Toast.LENGTH_LONG).show(); | |
// } | |
} | |
}); | |
// Click check break in | |
LinearLayout break_in = findViewById(R.id.break_in); | |
break_in.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
// if (hasDataStatusCheck && getStatus.equals("break_out")) { | |
Intent intent = new Intent(getApplicationContext(), SendingDataAbsentActivity.class); | |
intent.putExtra("COMMAND_MESSAGE", "break_in"); | |
startActivity(intent); | |
// } else { | |
// // Already check-in | |
// Toast.makeText(MainActivity.this, "Please Break-Out first.", Toast.LENGTH_LONG).show(); | |
// } | |
} | |
}); | |
// Click check overtime in | |
LinearLayout overtime_in = findViewById(R.id.overtime_in); | |
overtime_in.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
// if (hasDataStatusCheck && getStatus.equals("out_time") && getStatusOvertime.equals("yes")) { | |
Intent intent = new Intent(getApplicationContext(), SendingDataAbsentActivity.class); | |
intent.putExtra("COMMAND_MESSAGE", "overtime_in"); | |
startActivity(intent); | |
// } else { | |
// // Already check-in | |
// Toast.makeText(MainActivity.this, "Please Check-Out first.", Toast.LENGTH_LONG).show(); | |
// } | |
} | |
}); | |
// Click check overtime out | |
LinearLayout overtime_out = findViewById(R.id.overtime_out); | |
overtime_out.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
// if (hasDataStatusCheck && getStatus.equals("overtime_in")) { | |
Intent intent = new Intent(getApplicationContext(), SendingDataAbsentActivity.class); | |
intent.putExtra("COMMAND_MESSAGE", "overtime_out"); | |
startActivity(intent); | |
// } else { | |
// // Already check-in | |
// Toast.makeText(MainActivity.this, "Please Overtime-In first.", Toast.LENGTH_LONG).show(); | |
// } | |
} | |
}); | |
// Click check interruptions out | |
LinearLayout interruptions_out = findViewById(R.id.interruptions_out); | |
interruptions_out.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
// if (hasDataStatusCheck && getStatus.equals("in_time") || getStatus.equals("break_in")) { | |
Intent intent = new Intent(getApplicationContext(), SendingDataAbsentActivity.class); | |
intent.putExtra("COMMAND_MESSAGE", "interruptions_out"); | |
startActivity(intent); | |
// } else { | |
// // Already check-in | |
// Toast.makeText(MainActivity.this, "Please Check-In or Break-in first.", Toast.LENGTH_LONG).show(); | |
// } | |
} | |
}); | |
// Click check interruptions in | |
LinearLayout interruptions_in = findViewById(R.id.interruptions_in); | |
interruptions_in.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
// if (hasDataStatusCheck && getStatus.equals("interruptions_out")) { | |
Intent intent = new Intent(getApplicationContext(), SendingDataAbsentActivity.class); | |
intent.putExtra("COMMAND_MESSAGE", "interruptions_in"); | |
startActivity(intent); | |
// } else { | |
// // Already check-in | |
// Toast.makeText(MainActivity.this, "Please Check-In first.", Toast.LENGTH_LONG).show(); | |
// } | |
} | |
}); | |
// Click setting | |
LinearLayout settings = findViewById(R.id.settings); | |
settings.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Intent intent = new Intent(getApplicationContext(), SettingsActivity.class); | |
startActivity(intent); | |
} | |
}); | |
// Click check attendance | |
LinearLayout checkAttendnace = findViewById(R.id.checkAttendnace); | |
checkAttendnace.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Intent intent = new Intent(getApplicationContext(), CheckAttendanceActivity.class); | |
startActivity(intent); | |
} | |
}); | |
LinearLayout sendLocation = findViewById(R.id.sendLocation); | |
sendLocation.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Intent intent = new Intent(getApplicationContext(), SendRealTimeActivity.class); | |
startActivity(intent); | |
} | |
}); | |
LinearLayout location = findViewById(R.id.location); | |
location.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Intent intent = new Intent(getApplicationContext(), LocationMapsActivity.class); | |
startActivity(intent); | |
} | |
}); | |
setImage(); | |
} | |
public void setImage() { | |
if (checkIfImageExists(mContext, userName)) { | |
String filePath = getImage(mContext, userName).getAbsolutePath(); | |
Bitmap bitmap = BitmapFactory.decodeFile(filePath); | |
profilePicMain.setImageBitmap(bitmap); | |
} | |
} | |
// Create menu | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
MenuInflater inflater = getMenuInflater(); | |
inflater.inflate(R.menu.menu, menu); | |
return super.onCreateOptionsMenu(menu); | |
} | |
// Action menu | |
@Override | |
public boolean onOptionsItemSelected(final MenuItem item) { | |
if (item.getItemId() == R.id.about) { | |
startActivity(new Intent(MainActivity.this, AboutActivity.class)); | |
return true; | |
} else if (item.getItemId() == R.id.logout) { | |
//Remove a value from preference | |
PrefUtils.removeFromPrefs(getApplicationContext(), PrefKeys.IS_LOGGED); | |
Intent intent = new Intent(getApplicationContext(), LoginActivity.class); | |
MainActivity.this.startActivity(intent); | |
MainActivity.this.finish(); | |
return true; | |
} else { | |
Intent intent = new Intent(getApplicationContext(), ProfileActivity.class); | |
MainActivity.this.startActivity(intent); | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
// Get location lat long | |
public void locationFetched(Location mLocal, Location oldLocation, String time, String locationProvider) { | |
LatLongit(mLocal.getLatitude(), mLocal.getLongitude()); | |
} | |
// Get location for first time | |
public void LatLongit(double latitudeValue, double longitudeValue) { | |
latitude = latitudeValue; | |
longitude = longitudeValue; | |
} | |
// Update status on resume | |
protected void onResume() { | |
super.onResume(); | |
getStatus = (String) PrefUtils.getFromPrefs(this, PrefKeys.STATUS_CHECK, ""); | |
getStatusOvertime = (String) PrefUtils.getFromPrefs(this, PrefKeys.AVAILABLE_OVERTIME, ""); | |
setImage(); | |
} | |
public File getImage(Context context, String filename) { | |
File mediaImage = null; | |
try { | |
String root = context.getFilesDir().toString(); | |
File myDir = new File(root); | |
if (!myDir.exists()) | |
return null; | |
mediaImage = new File(myDir.getPath() + folderPath + filename + "_" + ".jpg"); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return mediaImage; | |
} | |
public boolean checkIfImageExists(Context context, String filename) { | |
Bitmap b = null; | |
File file = getImage(context, filename); | |
String path = file.getAbsolutePath(); | |
if (path != null) | |
b = BitmapFactory.decodeFile(path); | |
return b != null && !b.equals(""); | |
} | |
public void checkUrlKeyName() { | |
//check if empty database | |
if (dbManager.checkIfEmpty("DATASABSENT")) { | |
Toast.makeText(MainActivity.this, getString(R.string.please_setting_url_key), Toast.LENGTH_LONG).show(); | |
} | |
//check if not empty database | |
else if (!dbManager.checkIfEmpty("DATASABSENT")) { | |
// If DB is not empty | |
Cursor cursor = dbManager.getItem(); | |
if (cursor != null && cursor.moveToFirst()) { | |
linkDB = cursor.getString(cursor.getColumnIndex | |
(DatabaseHelper.LINK)); | |
keyDB = cursor.getString(cursor.getColumnIndex | |
(DatabaseHelper.KEY_API)); | |
userNameDB = cursor.getString(cursor.getColumnIndex | |
(DatabaseHelper.USER_NAME)); | |
userName = userNameDB.replaceAll(" ", "_").toLowerCase(); | |
idNumber = cursor.getString(cursor.getColumnIndex | |
(DatabaseHelper.ID_NUMBER)); | |
departmentDB = cursor.getString(cursor.getColumnIndex | |
(DatabaseHelper.DEPARTMENT)); | |
cursor.close(); | |
} | |
//check if null database url and key | |
if (linkDB.equals("") && keyDB.equals("")) { | |
Toast.makeText(MainActivity.this, getString(R.string.please_setting_url_key), Toast.LENGTH_LONG).show(); | |
} else if (linkDB.equals("")) { | |
Toast.makeText(MainActivity.this, getString(R.string.setting_url), Toast.LENGTH_LONG).show(); | |
} else if (keyDB.equals("")) { | |
Toast.makeText(MainActivity.this, getString(R.string.setting_key), Toast.LENGTH_LONG).show(); | |
} | |
} | |
} | |
protected void onStart() { | |
super.onStart(); | |
getStatus = (String) PrefUtils.getFromPrefs(this, PrefKeys.STATUS_CHECK, ""); | |
getStatusOvertime = (String) PrefUtils.getFromPrefs(this, PrefKeys.AVAILABLE_OVERTIME, ""); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment