From 654e2be93356a49660db7c413651cc1195121dc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Schu=CC=88ller?= Date: Tue, 1 Jan 2019 23:56:26 +0100 Subject: [PATCH] - Basic Login --- CHANGELOG.md | 4 + app/build.gradle | 4 +- .../peertube/activity/AccountActivity.java | 63 +++++++- .../peertube/activity/LoginActivity.java | 26 ++++ .../network/AuthorizationInterceptor.java | 20 ++- .../peertube/network/GetUserService.java | 2 +- app/src/main/res/layout/activity_account.xml | 29 +++- app/src/main/res/layout/activity_login.xml | 139 ++++++++++-------- 8 files changed, 212 insertions(+), 75 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fc7448a..a3e7768 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +### Version 1.0.20 Tag: v1.0.20 (2019-01-02) + * Added basic login framework + * AR Strings update (@rex07) + ### Version 1.0.19 Tag: v1.0.19 (2018-12-31) * Video Language Filter (@lishoujun) diff --git a/app/build.gradle b/app/build.gradle index 80729aa..7d6b93b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -6,8 +6,8 @@ android { applicationId "net.schueller.peertube" minSdkVersion 21 targetSdkVersion 28 - versionCode 1019 - versionName "1.0.19" + versionCode 1020 + versionName "1.0.20" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) diff --git a/app/src/main/java/net/schueller/peertube/activity/AccountActivity.java b/app/src/main/java/net/schueller/peertube/activity/AccountActivity.java index 095c1d4..755c5c3 100644 --- a/app/src/main/java/net/schueller/peertube/activity/AccountActivity.java +++ b/app/src/main/java/net/schueller/peertube/activity/AccountActivity.java @@ -29,16 +29,28 @@ import android.util.Log; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; +import android.widget.TextView; import com.mikepenz.fontawesome_typeface_library.FontAwesome; import com.mikepenz.iconics.IconicsDrawable; import net.schueller.peertube.R; +import net.schueller.peertube.helper.APIUrlHelper; +import net.schueller.peertube.model.Me; +import net.schueller.peertube.model.OauthClient; +import net.schueller.peertube.model.Token; +import net.schueller.peertube.network.AuthenticationService; +import net.schueller.peertube.network.GetUserService; +import net.schueller.peertube.network.RetrofitInstance; import net.schueller.peertube.network.Session; +import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.SearchView; import androidx.appcompat.widget.Toolbar; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; import static net.schueller.peertube.helper.Constants.DEFAULT_THEME; import static net.schueller.peertube.helper.Constants.THEME_PREF_KEY; @@ -46,6 +58,8 @@ import static net.schueller.peertube.helper.Constants.THEME_PREF_KEY; public class AccountActivity extends AppCompatActivity { + private static final String TAG = "AccountActivity"; + @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); @@ -70,6 +84,7 @@ public class AccountActivity extends AppCompatActivity { Intent intent = new Intent(this, LoginActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); this.startActivity(intent); + finish(); return true; default: break; @@ -78,6 +93,12 @@ public class AccountActivity extends AppCompatActivity { return super.onOptionsItemSelected(item); } + @Override + public boolean onSupportNavigateUp() { + finish(); // close this activity as oppose to navigating up + + return false; + } @Override protected void onCreate(Bundle savedInstanceState) { @@ -93,10 +114,16 @@ public class AccountActivity extends AppCompatActivity { setContentView(R.layout.activity_account); + // Attaching the layout to the toolbar object Toolbar toolbar = findViewById(R.id.tool_bar_user); // Setting toolbar as the ActionBar with setSupportActionBar() call setSupportActionBar(toolbar); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + getSupportActionBar().setHomeAsUpIndicator( + new IconicsDrawable(this, FontAwesome.Icon.faw_chevron_left).actionBar() + ); + init(); } @@ -110,7 +137,41 @@ public class AccountActivity extends AppCompatActivity { // TODO - return false; + + String apiBaseURL = APIUrlHelper.getUrlWithVersion(this); + + GetUserService service = RetrofitInstance.getRetrofitInstance(apiBaseURL).create(GetUserService.class); + + Call call = service.getMe(); + + call.enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + + if (response.isSuccessful()) { + + Me me = response.body(); + + TextView username = findViewById(R.id.account_username); + TextView email = findViewById(R.id.account_email); + + username.setText(me.getUsername()); + email.setText(me.getEmail()); + + Log.v(TAG, me.getEmail()); + + } + + + } + + @Override + public void onFailure(Call call, Throwable t) { + + } + }); + + return true; } @Override diff --git a/app/src/main/java/net/schueller/peertube/activity/LoginActivity.java b/app/src/main/java/net/schueller/peertube/activity/LoginActivity.java index 9ec1427..a1cc45c 100644 --- a/app/src/main/java/net/schueller/peertube/activity/LoginActivity.java +++ b/app/src/main/java/net/schueller/peertube/activity/LoginActivity.java @@ -27,6 +27,10 @@ import android.util.Log; import android.widget.AutoCompleteTextView; import android.widget.Button; import android.widget.EditText; +import android.widget.Toast; + +import com.mikepenz.fontawesome_typeface_library.FontAwesome; +import com.mikepenz.iconics.IconicsDrawable; import net.schueller.peertube.R; import net.schueller.peertube.helper.APIUrlHelper; @@ -37,6 +41,7 @@ import net.schueller.peertube.network.RetrofitInstance; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -73,8 +78,24 @@ public class LoginActivity extends AppCompatActivity { mEmailView = findViewById(R.id.email); mPasswordView = findViewById(R.id.password); + + // Attaching the layout to the toolbar object + Toolbar toolbar = findViewById(R.id.tool_bar_login); + // Setting toolbar as the ActionBar with setSupportActionBar() call + setSupportActionBar(toolbar); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + getSupportActionBar().setHomeAsUpIndicator( + new IconicsDrawable(this, FontAwesome.Icon.faw_chevron_left).actionBar() + ); + } + @Override + public boolean onSupportNavigateUp() { + finish(); // close this activity as oppose to navigating up + + return false; + } private void attemptLogin() { @@ -139,8 +160,13 @@ public class LoginActivity extends AppCompatActivity { intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); context.startActivity(intent); + finish(); // close this activity + } else { Log.wtf(TAG, response2.toString()); + + Toast.makeText(LoginActivity.this, "Login Error!", Toast.LENGTH_LONG).show(); + } } diff --git a/app/src/main/java/net/schueller/peertube/network/AuthorizationInterceptor.java b/app/src/main/java/net/schueller/peertube/network/AuthorizationInterceptor.java index 438797e..37786c7 100644 --- a/app/src/main/java/net/schueller/peertube/network/AuthorizationInterceptor.java +++ b/app/src/main/java/net/schueller/peertube/network/AuthorizationInterceptor.java @@ -31,6 +31,8 @@ public class AuthorizationInterceptor implements Interceptor { public AuthorizationInterceptor() { } + + @Override public Response intercept(Chain chain) throws IOException { @@ -41,18 +43,20 @@ public class AuthorizationInterceptor implements Interceptor { if (session.isLoggedIn()) { - if (mainResponse.code() == 401 || mainResponse.code() == 403) { -// session.invalidate(); -// return mainResponse; - Log.v("Authorization", "Intercept code: " + mainResponse.code()); - - } - // add authentication header to each request if we are logged in Request.Builder builder = mainRequest.newBuilder().header("Authorization", session.getToken()). method(mainRequest.method(), mainRequest.body()); + // Log.v("Authorization", "Intercept: " + session.getToken()); + + // build request mainResponse = chain.proceed(builder.build()); - Log.v("Authorization", "Intercept: " + session.getToken()); + + // logout on auth error + if (mainResponse.code() == 401 || mainResponse.code() == 403) { + session.invalidate(); + Log.v("Authorization", "Intercept: Logout forced"); + } + } return mainResponse; diff --git a/app/src/main/java/net/schueller/peertube/network/GetUserService.java b/app/src/main/java/net/schueller/peertube/network/GetUserService.java index 7c4d459..850b9f5 100644 --- a/app/src/main/java/net/schueller/peertube/network/GetUserService.java +++ b/app/src/main/java/net/schueller/peertube/network/GetUserService.java @@ -11,7 +11,7 @@ import retrofit2.http.Query; public interface GetUserService { @GET("users/me") - Call getMe(@Header("Authorization") String authorization); + Call getMe(); @GET("users/me/subscriptions/videos") Call getVideosSubscripions( diff --git a/app/src/main/res/layout/activity_account.xml b/app/src/main/res/layout/activity_account.xml index bc4f3a5..bfa68ce 100644 --- a/app/src/main/res/layout/activity_account.xml +++ b/app/src/main/res/layout/activity_account.xml @@ -1,10 +1,11 @@ - + tools:context=".activity.AccountActivity" + android:orientation="vertical"> - \ No newline at end of file + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_login.xml b/app/src/main/res/layout/activity_login.xml index 9187735..6ea1ec6 100644 --- a/app/src/main/res/layout/activity_login.xml +++ b/app/src/main/res/layout/activity_login.xml @@ -3,74 +3,93 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" - android:orientation="vertical" - android:paddingBottom="@dimen/activity_vertical_margin" - android:paddingLeft="@dimen/activity_horizontal_margin" - android:paddingRight="@dimen/activity_horizontal_margin" - android:paddingTop="@dimen/activity_vertical_margin" - tools:context="net.schueller.peertube.activity.LoginActivity"> + android:orientation="vertical"> - - - + android:orientation="vertical"> - - - + android:elevation="4dp" /> - + + + + + + android:layout_height="match_parent"> - - - - - - - - - - - -