diff --git a/android/app/google-services.json b/android/app/google-services.json new file mode 100644 index 0000000000000000000000000000000000000000..6e04b064c6d0e8b4ff0833c37ce17b8d28d148b1 --- /dev/null +++ b/android/app/google-services.json @@ -0,0 +1,42 @@ +{ + "project_info": { + "project_number": "413062091488", + "firebase_url": "https://chatman-a99e6.firebaseio.com", + "project_id": "chatman-a99e6", + "storage_bucket": "chatman-a99e6.appspot.com" + }, + "client": [ + { + "client_info": { + "mobilesdk_app_id": "1:413062091488:android:71042f72808d827c", + "android_client_info": { + "package_name": "com.chatman" + } + }, + "oauth_client": [ + { + "client_id": "413062091488-sdfrjk51lo494mel9bbfj38rqlm7b6vj.apps.googleusercontent.com", + "client_type": 3 + } + ], + "api_key": [ + { + "current_key": "AIzaSyBaahgA_2CnppVu0QVd21aNX_C7HVAccE0" + } + ], + "services": { + "analytics_service": { + "status": 1 + }, + "appinvite_service": { + "status": 1, + "other_platform_oauth_client": [] + }, + "ads_service": { + "status": 2 + } + } + } + ], + "configuration_version": "1" +} \ No newline at end of file diff --git a/android/app/src/main/java/com/chatman/AuthActivity.java b/android/app/src/main/java/com/chatman/AuthActivity.java new file mode 100644 index 0000000000000000000000000000000000000000..c106b447d24cf58a3ac8ec6f9aec2a95162a776f --- /dev/null +++ b/android/app/src/main/java/com/chatman/AuthActivity.java @@ -0,0 +1,212 @@ +package com.etsi; + +import android.content.Intent; +import android.content.SharedPreferences; +import android.support.annotation.NonNull; +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.support.v7.widget.CardView; +import android.transition.TransitionManager; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.LinearLayout; +import android.widget.TextView; +import android.widget.Toast; + +import com.etsi.helper.FirebaseHelper; +import com.etsi.helper.PreferencesHelper; +import com.etsi.model.Kreator; +import com.etsi.model.Sobat; +import com.google.firebase.database.DataSnapshot; +import com.google.firebase.database.DatabaseError; +import com.google.firebase.database.ValueEventListener; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +public class AuthActivity extends AppCompatActivity { + + // view component + private LinearLayout authContainer; + private CardView nameContainer; + private CardView confirmContainer; + private EditText emailEditText; + private EditText passwordEditText; + private EditText confirmEditText; + private EditText nameEditText; + private Button loginButton; + private Button registerButton; + private TextView switchTextView; + + + // activity state + private boolean isLoginView = true; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_auth); + bindView(); + switchToLogin(); + } + + // bind all component view with it's xml + private void bindView() { + authContainer = findViewById(R.id.auth_container); + emailEditText = findViewById(R.id.auth_email); + passwordEditText = findViewById(R.id.auth_password); + confirmEditText = findViewById(R.id.auth_confirm); + nameEditText = findViewById(R.id.auth_name); + loginButton = findViewById(R.id.auth_login); + registerButton = findViewById(R.id.auth_register); + switchTextView = findViewById(R.id.auth_switch); + } + + // do login + public void login(View view) { + final String email = emailEditText.getText().toString().trim(); + final String password = passwordEditText.getText().toString().trim(); + + if (email.endsWith("@etsi.com")) { + FirebaseHelper.dbKreator.addListenerForSingleValueEvent(new ValueEventListener() { + @Override + public void onDataChange(@NonNull DataSnapshot dataSnapshot) { + if (!loginAsKreator(dataSnapshot, email, password)) { + Toast.makeText(AuthActivity.this, "Email dan password tidak benar", Toast.LENGTH_SHORT).show(); + } else { + + } + } + + @Override + public void onCancelled(@NonNull DatabaseError databaseError) { + + } + }); + } else { + FirebaseHelper.dbSobat.addListenerForSingleValueEvent(new ValueEventListener() { + @Override + public void onDataChange(@NonNull DataSnapshot dataSnapshot) { + if (!loginAsSobat(dataSnapshot, email, password)) { + Toast.makeText(AuthActivity.this, "Email dan password tidak benar", Toast.LENGTH_SHORT).show(); + } + } + + @Override + public void onCancelled(@NonNull DatabaseError databaseError) { + + } + }); + } + } + + private boolean loginAsKreator(DataSnapshot dataSnapshot, String email, String password) { + for (DataSnapshot ds : dataSnapshot.getChildren()) { + Kreator kreator = ds.getValue(Kreator.class); + if (kreator.getEmail().equals(email) && kreator.getPassword().equals(md5(password))) { + PreferencesHelper.setUserFirebaseKey(this, ds.getKey()); + PreferencesHelper.setCurrentKreatorKey(ds.getKey()); + PreferencesHelper.setUserRole(this, PreferencesHelper.USER_ROLE_KREATOR); + startActivity(new Intent(this, MainActivity.class)); + return true; + } + } + return false; + } + + private boolean loginAsSobat(DataSnapshot dataSnapshot, String email, String password) { + for (DataSnapshot ds : dataSnapshot.getChildren()) { + Sobat sobat = ds.getValue(Sobat.class); + if (sobat.getEmail().equals(email) && sobat.getPassword().equals(md5(password))) { + PreferencesHelper.setUserFirebaseKey(this, ds.getKey()); + PreferencesHelper.setUserRole(this, PreferencesHelper.USER_ROLE_SOBAT); + startActivity(new Intent(this, MainActivity.class)); + return true; + } + } + return false; + } + + private String md5(final String s) { + final String MD5 = "MD5"; + try { + // Create MD5 Hash + MessageDigest digest = java.security.MessageDigest.getInstance(MD5); + digest.update(s.getBytes()); + byte messageDigest[] = digest.digest(); + + // Create Hex String + StringBuilder hexString = new StringBuilder(); + for (byte aMessageDigest : messageDigest) { + String h = Integer.toHexString(0xFF & aMessageDigest); + while (h.length() < 2) + h = "0" + h; + hexString.append(h); + } + return hexString.toString(); + + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } + return ""; + } + + // do register + public void register(View view) { + String name = nameEditText.getText().toString().trim(); + String email = emailEditText.getText().toString().trim(); + String password = passwordEditText.getText().toString().trim(); + String confirm = confirmEditText.getText().toString().trim(); + + if (!password.equals(confirm)) { + Toast.makeText(this, "Konfirmasi password tidak cocok", Toast.LENGTH_SHORT).show(); + return; + } + + if (email.endsWith("@etsi.com")) { + String key = FirebaseHelper.dbKreator.push().getKey(); + Kreator kreator = new Kreator(key, name, email, md5(password)); + kreator.setKey(key); + PreferencesHelper.setUserRole(this, PreferencesHelper.USER_ROLE_KREATOR); + PreferencesHelper.setUserFirebaseKey(this, key); + FirebaseHelper.dbKreator.child(key).setValue(kreator); + } else { + String key = FirebaseHelper.dbSobat.push().getKey(); + Sobat sobat = new Sobat(key, name, email, md5(password)); + sobat.setKey(key); + PreferencesHelper.setUserRole(this, PreferencesHelper.USER_ROLE_SOBAT); + PreferencesHelper.setUserFirebaseKey(this, key); + FirebaseHelper.dbSobat.child(key).setValue(sobat); + } + startActivity(new Intent(this, MainActivity.class)); + } + + // switch between login and register + public void switchAction(View view) { + isLoginView = !isLoginView; + if (isLoginView) { + switchToLogin(); + } else { + switchToRegister(); + } + } + + private void switchToLogin() { + TransitionManager.beginDelayedTransition(authContainer); + nameEditText.setVisibility(View.GONE); + confirmEditText.setVisibility(View.GONE); + loginButton.setVisibility(View.VISIBLE); + registerButton.setVisibility(View.GONE); + switchTextView.setText(getString(R.string.register)); + } + + private void switchToRegister() { + TransitionManager.beginDelayedTransition(authContainer); + nameEditText.setVisibility(View.VISIBLE); + confirmEditText.setVisibility(View.VISIBLE); + loginButton.setVisibility(View.GONE); + registerButton.setVisibility(View.VISIBLE); + switchTextView.setText(getString(R.string.login)); + } +} \ No newline at end of file diff --git a/android/app/src/main/java/com/chatman/helper/FirebaseHelper.java b/android/app/src/main/java/com/chatman/helper/FirebaseHelper.java new file mode 100644 index 0000000000000000000000000000000000000000..8e7905a489eb06d2255c33b78592fa447c947b21 --- /dev/null +++ b/android/app/src/main/java/com/chatman/helper/FirebaseHelper.java @@ -0,0 +1,13 @@ +package com.chatman.helper; + +import com.google.firebase.database.DatabaseReference; +import com.google.firebase.database.FirebaseDatabase; + +public class FirebaseHelper { + + private static final String DB_USER = "user"; + + public static String USER_KEY, USER_EMAIL, USER_NAMA, USER_PASSWORD; + private static DatabaseReference reference = FirebaseDatabase.getInstance().getReference(); + public static final DatabaseReference dbUser = reference.child(DB_USER); +} diff --git a/android/app/src/main/java/com/chatman/helper/PreferencesHelper.java b/android/app/src/main/java/com/chatman/helper/PreferencesHelper.java new file mode 100644 index 0000000000000000000000000000000000000000..29d81df812b97b995eb995049ff12351700ab62c --- /dev/null +++ b/android/app/src/main/java/com/chatman/helper/PreferencesHelper.java @@ -0,0 +1,93 @@ +package com.etsi.helper; + +import android.content.Context; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; + +public class PreferencesHelper { + + private static final String USER_ROLE_KEY = "user_role_key"; + public static final boolean USER_ROLE_KREATOR = true; + public static final boolean USER_ROLE_SOBAT = false; + public static final String USER_FIREBASE_KEY = "user_firebase_key"; + public static final String KREATOR1_TIER_KEY = "kreator1_tier_key"; + public static final String KREATOR2_TIER_KEY = "kreator2_tier_key"; + public static String CURRENT_KREATOR_KEY; + + private static void setBooleanPreferences(Context context, String key, boolean value) { + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + SharedPreferences.Editor editor = preferences.edit(); + editor.putBoolean(key, value); + editor.apply(); + } + + private static boolean getBooleanPreferences(Context context, String key) { + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + return preferences.getBoolean(key, false); + } + + private static void setStringPreferences(Context context, String key, String value) { + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + SharedPreferences.Editor editor = preferences.edit(); + editor.putString(key, value); + editor.apply(); + } + + private static String getStringPreferences(Context context, String key) { + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + return preferences.getString(key, ""); + } + + private static void setIntPreferences(Context context, String key, int value) { + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + SharedPreferences.Editor editor = preferences.edit(); + editor.putInt(key, value); + editor.apply(); + } + + private static int getIntPreferences(Context context, String key) { + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + return preferences.getInt(key, 0); + } + + public static void setUserRole(Context context, boolean value) { + setBooleanPreferences(context, USER_ROLE_KEY, value); + } + + public static boolean getUserRole(Context context) { + return getBooleanPreferences(context, USER_ROLE_KEY); + } + + public static void setUserFirebaseKey(Context context, String value) { + setStringPreferences(context, USER_FIREBASE_KEY, value); + } + + public static String getUserFirebaseKey(Context context) { + return getStringPreferences(context, USER_FIREBASE_KEY); + } + + public static void setKreator1TierKey(Context context, int value){ + setIntPreferences(context, KREATOR1_TIER_KEY, value); + } + + public static int getKreator1TierKey(Context context){ + return getIntPreferences(context, KREATOR1_TIER_KEY); + } + + public static void setKreator2TierKey(Context context, int value){ + setIntPreferences(context, KREATOR2_TIER_KEY, value); + } + + public static int getKreator2TierKey(Context context){ + return getIntPreferences(context, KREATOR2_TIER_KEY); + } + + + public static String getCurrentKreatorKey() { + return CURRENT_KREATOR_KEY; + } + + public static void setCurrentKreatorKey(String value) { + CURRENT_KREATOR_KEY = value; + } +} diff --git a/android/app/src/main/java/com/chatman/model/User.java b/android/app/src/main/java/com/chatman/model/User.java new file mode 100644 index 0000000000000000000000000000000000000000..d3af306b8cb869a828a8c838b73abcaa03d020f6 --- /dev/null +++ b/android/app/src/main/java/com/chatman/model/User.java @@ -0,0 +1,4 @@ +package com.chatman.model; + +public class User { +} diff --git a/android/app/src/main/res/drawable/ic_arrow_back_black_24dp.xml b/android/app/src/main/res/drawable/ic_arrow_back_black_24dp.xml new file mode 100644 index 0000000000000000000000000000000000000000..beafea3959ebe8990c2567f7a785c00b3a2330b4 --- /dev/null +++ b/android/app/src/main/res/drawable/ic_arrow_back_black_24dp.xml @@ -0,0 +1,9 @@ +<vector xmlns:android="http://schemas.android.com/apk/res/android" + android:width="24dp" + android:height="24dp" + android:viewportWidth="24.0" + android:viewportHeight="24.0"> + <path + android:fillColor="#FF000000" + android:pathData="M20,11H7.83l5.59,-5.59L12,4l-8,8 8,8 1.41,-1.41L7.83,13H20v-2z"/> +</vector>