|
|
|
@ -1,22 +1,49 @@
|
|
|
|
|
package dev.garrettmills.starship.hyperlink;
|
|
|
|
|
|
|
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
|
|
|
|
|
|
import android.Manifest;
|
|
|
|
|
import android.app.NotificationChannel;
|
|
|
|
|
import android.app.NotificationManager;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
|
import android.database.Cursor;
|
|
|
|
|
import android.os.Bundle;
|
|
|
|
|
import android.provider.Telephony;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
|
import androidx.core.app.ActivityCompat;
|
|
|
|
|
import androidx.core.content.ContextCompat;
|
|
|
|
|
|
|
|
|
|
import com.android.volley.toolbox.Volley;
|
|
|
|
|
|
|
|
|
|
import dev.garrettmills.starship.hyperlink.util.APIv1;
|
|
|
|
|
import dev.garrettmills.starship.hyperlink.util.AccessToken;
|
|
|
|
|
import dev.garrettmills.starship.hyperlink.util.InvalidLoginTokenException;
|
|
|
|
|
import dev.garrettmills.starship.hyperlink.util.LoginToken;
|
|
|
|
|
import dev.garrettmills.starship.hyperlink.util.NotAuthenticatedException;
|
|
|
|
|
|
|
|
|
|
public class MainActivity extends AppCompatActivity {
|
|
|
|
|
TextView serverAddressView;
|
|
|
|
|
Intent serviceIntent = null;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
Hyperlink.preferences = getSharedPreferences(Hyperlink.SHARED_PREFERENCES_NAME, MODE_PRIVATE);
|
|
|
|
|
Hyperlink.httpRequestQueue = Volley.newRequestQueue(this);
|
|
|
|
|
|
|
|
|
|
setContentView(R.layout.activity_main);
|
|
|
|
|
NotificationChannel channel = new NotificationChannel(Hyperlink.NOTIFICATION_CHANNEL, "Starship Hyperlink", NotificationManager.IMPORTANCE_LOW);
|
|
|
|
|
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
|
assert manager != null;
|
|
|
|
|
manager.createNotificationChannel(channel);
|
|
|
|
|
|
|
|
|
|
if (APIv1.isAuthenticated()) switchToStatusMode();
|
|
|
|
|
else switchToLoginMode();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onScanTokenClick(View view) {
|
|
|
|
@ -29,6 +56,76 @@ public class MainActivity extends AppCompatActivity {
|
|
|
|
|
startActivityForResult(intent, Hyperlink.REQUEST_LOGIN_TOKEN);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onLogoutButtonClick(View view) {
|
|
|
|
|
switchToLoginMode();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void switchToStatusMode() {
|
|
|
|
|
setContentView(R.layout.activity_main_status);
|
|
|
|
|
serverAddressView = findViewById(R.id.activity_main_serverAddressTextView);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
AccessToken token = APIv1.getToken();
|
|
|
|
|
serverAddressView.setText(token.getServer());
|
|
|
|
|
} catch (NotAuthenticatedException e) {
|
|
|
|
|
if ( BuildConfig.DEBUG ) {
|
|
|
|
|
Toast.makeText(getApplicationContext(), "Cannot fetch token; not authenticated.", Toast.LENGTH_SHORT).show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
APIv1.logout();
|
|
|
|
|
switchToLoginMode();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
requestSMSRead();
|
|
|
|
|
Cursor cursor = getContentResolver().query(Telephony.Threads.CONTENT_URI, null, null, null, "date DESC");
|
|
|
|
|
if ( cursor.moveToFirst() ) {
|
|
|
|
|
int j = 0;
|
|
|
|
|
do {
|
|
|
|
|
j += 1;
|
|
|
|
|
if ( j > 5 ) break;
|
|
|
|
|
for ( int i = 0; i < cursor.getColumnCount(); i += 1 ) {
|
|
|
|
|
Log.i("MainActivity", cursor.getColumnName(i) + ": " + cursor.getString(i));
|
|
|
|
|
}
|
|
|
|
|
} while ( cursor.moveToNext() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
serviceIntent = new Intent(this, MessagingService.class);
|
|
|
|
|
ContextCompat.startForegroundService(this, serviceIntent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void switchToLoginMode() {
|
|
|
|
|
APIv1.logout();
|
|
|
|
|
|
|
|
|
|
if ( serviceIntent != null ) stopService(serviceIntent);
|
|
|
|
|
|
|
|
|
|
setContentView(R.layout.activity_main);
|
|
|
|
|
serverAddressView = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void requestSMSRead() {
|
|
|
|
|
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_SMS) == PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
// switchToStatusMode();
|
|
|
|
|
} else {
|
|
|
|
|
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_SMS}, Hyperlink.REQUEST_PERMISSION_READ_SMS);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
|
|
|
|
if (requestCode == Hyperlink.REQUEST_PERMISSION_READ_SMS) {
|
|
|
|
|
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
switchToStatusMode();
|
|
|
|
|
} else {
|
|
|
|
|
if ( BuildConfig.DEBUG ) {
|
|
|
|
|
Toast.makeText(this, "Read SMS permission denied", Toast.LENGTH_SHORT).show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switchToLoginMode();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
|
|
@ -38,6 +135,15 @@ public class MainActivity extends AppCompatActivity {
|
|
|
|
|
if ( BuildConfig.DEBUG ) {
|
|
|
|
|
Toast.makeText(getApplicationContext(), data.getExtras().getString(Hyperlink.EXTRA_LOGIN_TOKEN), Toast.LENGTH_SHORT).show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
LoginToken login = new LoginToken(data.getExtras().getString(Hyperlink.EXTRA_LOGIN_TOKEN));
|
|
|
|
|
APIv1.login(login);
|
|
|
|
|
switchToStatusMode();
|
|
|
|
|
} catch (InvalidLoginTokenException e) {
|
|
|
|
|
Toast.makeText(getApplicationContext(), "Invalid login token!", Toast.LENGTH_SHORT).show();
|
|
|
|
|
switchToLoginMode();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|