Thorium-android-app/app/src/main/java/net/schueller/peertube/activity/AccountActivity.java

176 lines
5.1 KiB
Java
Raw Normal View History

2018-12-29 22:10:13 +01:00
/*
* Copyright 2018 Stefan Schüller <sschueller@techdroid.com>
*
* License: GPL-3.0+
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.schueller.peertube.activity;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
2019-01-01 23:56:26 +01:00
import android.widget.TextView;
2018-12-29 22:10:13 +01:00
import com.mikepenz.fontawesome_typeface_library.FontAwesome;
import com.mikepenz.iconics.IconicsDrawable;
import net.schueller.peertube.R;
2019-01-01 23:56:26 +01:00
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;
2018-12-29 22:10:13 +01:00
import net.schueller.peertube.network.Session;
2019-01-01 23:56:26 +01:00
import androidx.annotation.NonNull;
2018-12-29 22:10:13 +01:00
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.Toolbar;
2019-01-01 23:56:26 +01:00
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
2018-12-29 22:10:13 +01:00
import static net.schueller.peertube.helper.Constants.DEFAULT_THEME;
import static net.schueller.peertube.helper.Constants.THEME_PREF_KEY;
2019-01-06 14:13:05 +01:00
public class AccountActivity extends CommonActivity {
2018-12-29 22:10:13 +01:00
2019-01-01 23:56:26 +01:00
private static final String TAG = "AccountActivity";
2018-12-29 22:10:13 +01:00
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_top_user, menu);
// Set an icon in the ActionBar
menu.findItem(R.id.action_logout).setIcon(
new IconicsDrawable(this, FontAwesome.Icon.faw_sign_out_alt).actionBar());
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// action with ID action_refresh was selected
case R.id.action_logout:
Session.getInstance().invalidate();
Intent intent = new Intent(this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
this.startActivity(intent);
2019-01-01 23:56:26 +01:00
finish();
2018-12-29 22:10:13 +01:00
return true;
default:
break;
}
return super.onOptionsItemSelected(item);
}
2019-01-01 23:56:26 +01:00
@Override
public boolean onSupportNavigateUp() {
finish(); // close this activity as oppose to navigating up
return false;
}
2018-12-29 22:10:13 +01:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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);
2019-01-01 23:56:26 +01:00
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(
new IconicsDrawable(this, FontAwesome.Icon.faw_chevron_left).actionBar()
);
2018-12-29 22:10:13 +01:00
init();
}
private void init() {
// try to get user data
getUserData();
}
private boolean getUserData() {
// TODO
2019-01-01 23:56:26 +01:00
String apiBaseURL = APIUrlHelper.getUrlWithVersion(this);
GetUserService service = RetrofitInstance.getRetrofitInstance(apiBaseURL).create(GetUserService.class);
Call<Me> call = service.getMe();
call.enqueue(new Callback<Me>() {
@Override
public void onResponse(@NonNull Call<Me> call, @NonNull Response<Me> 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<Me> call, Throwable t) {
}
});
return true;
2018-12-29 22:10:13 +01:00
}
@Override
protected void onResume() {
super.onResume();
init();
}
}