Skip to content
Snippets Groups Projects
Commit 12bc5826 authored by Michi302's avatar Michi302
Browse files

Merge branch 'master' into fdroid

# Conflicts:
#	app/build.gradle
#	app/src/main/java/com/commit451/gitlab/App.java
parents b7633464 230cbe28
No related branches found
No related tags found
No related merge requests found
Pipeline #
Showing
with 282 additions and 241 deletions
Loading
Loading
@@ -18,17 +18,22 @@ import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputLayout;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.AppCompatAutoCompleteTextView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.util.Patterns;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.TextView;
 
import com.commit451.gitlab.App;
import com.commit451.gitlab.BuildConfig;
import com.commit451.gitlab.R;
import com.commit451.gitlab.api.GitLabClient;
import com.commit451.gitlab.api.GitLab;
import com.commit451.gitlab.api.GitLabFactory;
import com.commit451.gitlab.api.OkHttpClientFactory;
import com.commit451.gitlab.data.Prefs;
import com.commit451.gitlab.dialog.HttpLoginDialog;
import com.commit451.gitlab.event.LoginEvent;
Loading
Loading
@@ -61,6 +66,8 @@ import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Credentials;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
Loading
Loading
@@ -85,8 +92,8 @@ public class LoginActivity extends BaseActivity {
 
@BindView(R.id.root)
View mRoot;
@BindView(R.id.close)
View mClose;
@BindView(R.id.toolbar)
Toolbar mToolbar;
@BindView(R.id.url_hint)
TextInputLayout mUrlHint;
@BindView(R.id.url_input)
Loading
Loading
@@ -122,26 +129,6 @@ public class LoginActivity extends BaseActivity {
}
};
 
@OnClick(R.id.close)
public void onCloseClick() {
onBackPressed();
}
@OnClick(R.id.show_normal_link)
public void showNormalLogin(TextView loginTypeTextView) {
if (mNormalLogin.getVisibility() == View.VISIBLE) {
mNormalLogin.setVisibility(View.GONE);
mTokenLogin.setVisibility(View.VISIBLE);
loginTypeTextView.setText(R.string.normal_link);
mIsNormalLogin = false;
} else {
mNormalLogin.setVisibility(View.VISIBLE);
mTokenLogin.setVisibility(View.GONE);
loginTypeTextView.setText(R.string.token_link);
mIsNormalLogin = true;
}
}
@OnClick(R.id.login_button)
public void onLoginClick() {
mTeleprinter.hideKeyboard();
Loading
Loading
@@ -203,7 +190,7 @@ public class LoginActivity extends BaseActivity {
 
private void login() {
// This seems useless - But believe me, it makes everything work! Don't remove it.
// (OkHttpClientProvider caches the clients and needs a new account to recreate them)
// (OkHttpClientFactory caches the clients and needs a new account to recreate them)
 
Account newAccount = new Account();
newAccount.setServerUrl(mAccount.getServerUrl());
Loading
Loading
@@ -290,7 +277,7 @@ public class LoginActivity extends BaseActivity {
mAccount.setUser(response.body());
mAccount.setLastUsed(new Date());
Prefs.addAccount(LoginActivity.this, mAccount);
GitLabClient.setAccount(mAccount);
App.instance().setAccount(mAccount);
App.bus().post(new LoginEvent(mAccount));
//This is mostly for if projects already exists, then we will reload the data
App.bus().post(new ReloadDataEvent());
Loading
Loading
@@ -314,7 +301,38 @@ public class LoginActivity extends BaseActivity {
mTeleprinter = new Teleprinter(this);
boolean showClose = getIntent().getBooleanExtra(EXTRA_SHOW_CLOSE, false);
 
mClose.setVisibility(showClose ? View.VISIBLE : View.GONE);
mToolbar.inflateMenu(R.menu.menu_login);
mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_private_token:
boolean isNormalLogin = mNormalLogin.getVisibility() == View.VISIBLE;
if (isNormalLogin) {
mNormalLogin.setVisibility(View.GONE);
mTokenLogin.setVisibility(View.VISIBLE);
item.setTitle(R.string.normal_link);
mIsNormalLogin = false;
} else {
mNormalLogin.setVisibility(View.VISIBLE);
mTokenLogin.setVisibility(View.GONE);
item.setTitle(R.string.token_link);
mIsNormalLogin = true;
}
return true;
}
return false;
}
});
if (showClose) {
mToolbar.setNavigationIcon(R.drawable.ic_close_24dp);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onBackPressed();
}
});
}
mPasswordInput.setOnEditorActionListener(onEditorActionListener);
mTokenInput.setOnEditorActionListener(onEditorActionListener);
mUserInput.setOnFocusChangeListener(new View.OnFocusChangeListener() {
Loading
Loading
@@ -360,10 +378,15 @@ public class LoginActivity extends BaseActivity {
}
 
private void connectByAuth() {
OkHttpClient.Builder gitlabClientBuilder = OkHttpClientFactory.create(mAccount);
if (BuildConfig.DEBUG) {
gitlabClientBuilder.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY));
}
GitLab gitLab = GitLabFactory.create(mAccount, gitlabClientBuilder.build());
if (mUserInput.getText().toString().contains("@")) {
GitLabClient.create(mAccount).loginWithEmail(mUserInput.getText().toString(), mPasswordInput.getText().toString()).enqueue(mLoginCallback);
gitLab.loginWithEmail(mUserInput.getText().toString(), mPasswordInput.getText().toString()).enqueue(mLoginCallback);
} else {
GitLabClient.create(mAccount).loginWithUsername(mUserInput.getText().toString(), mPasswordInput.getText().toString()).enqueue(mLoginCallback);
gitLab.loginWithUsername(mUserInput.getText().toString(), mPasswordInput.getText().toString()).enqueue(mLoginCallback);
}
}
 
Loading
Loading
@@ -373,7 +396,12 @@ public class LoginActivity extends BaseActivity {
}
 
private void loadUser() {
GitLabClient.create(mAccount).getThisUser().enqueue(mTestUserCallback);
OkHttpClient.Builder gitlabClientBuilder = OkHttpClientFactory.create(mAccount);
if (BuildConfig.DEBUG) {
gitlabClientBuilder.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY));
}
GitLab gitLab = GitLabFactory.create(mAccount, gitlabClientBuilder.build());
gitLab.getThisUser().enqueue(mTestUserCallback);
}
 
private void handleConnectionError(Throwable t) {
Loading
Loading
Loading
Loading
@@ -16,7 +16,7 @@ import com.commit451.gitlab.App;
import com.commit451.gitlab.R;
import com.commit451.gitlab.adapter.MergeRequestSectionsPagerAdapter;
import com.commit451.easycallback.EasyCallback;
import com.commit451.gitlab.api.GitLabClient;
import com.commit451.gitlab.api.GitLabFactory;
import com.commit451.gitlab.event.MergeRequestChangedEvent;
import com.commit451.gitlab.model.api.MergeRequest;
import com.commit451.gitlab.model.api.Project;
Loading
Loading
@@ -80,7 +80,7 @@ public class MergeRequestActivity extends BaseActivity {
switch (item.getItemId()) {
case R.id.action_merge:
mProgress.setVisibility(View.VISIBLE);
GitLabClient.instance().acceptMergeRequest(mProject.getId(), mMergeRequest.getId()).enqueue(mMergeRequestCallback);
App.instance().getGitLab().acceptMergeRequest(mProject.getId(), mMergeRequest.getId()).enqueue(mMergeRequestCallback);
return true;
}
return false;
Loading
Loading
Loading
Loading
@@ -15,12 +15,11 @@ import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
 
import com.commit451.easycallback.EasyCallback;
import com.commit451.gitlab.App;
import com.commit451.gitlab.R;
import com.commit451.gitlab.adapter.DividerItemDecoration;
import com.commit451.gitlab.adapter.MilestoneIssuesAdapter;
import com.commit451.easycallback.EasyCallback;
import com.commit451.gitlab.api.GitLabClient;
import com.commit451.gitlab.event.MilestoneChangedEvent;
import com.commit451.gitlab.model.api.Issue;
import com.commit451.gitlab.model.api.Milestone;
Loading
Loading
@@ -76,8 +75,8 @@ public class MilestoneActivity extends BaseActivity {
EventReceiver mEventReceiver;
 
@OnClick(R.id.add)
void onAddClick() {
Navigator.navigateToAddIssue(MilestoneActivity.this, null, mProject);
void onAddClick(View fab) {
Navigator.navigateToAddIssue(MilestoneActivity.this, fab, mProject);
}
 
@OnClick(R.id.edit)
Loading
Loading
@@ -237,7 +236,7 @@ public class MilestoneActivity extends BaseActivity {
}
}
});
GitLabClient.instance().getMilestoneIssues(mProject.getId(), mMilestone.getId()).enqueue(mIssuesCallback);
App.instance().getGitLab().getMilestoneIssues(mProject.getId(), mMilestone.getId()).enqueue(mIssuesCallback);
}
 
private void loadMore() {
Loading
Loading
@@ -249,16 +248,16 @@ public class MilestoneActivity extends BaseActivity {
mLoading = true;
 
Timber.d("loadMore called for %s", mNextPageUrl);
GitLabClient.instance().getMilestoneIssues(mNextPageUrl.toString()).enqueue(mMoreIssuesCallback);
App.instance().getGitLab().getMilestoneIssues(mNextPageUrl.toString()).enqueue(mMoreIssuesCallback);
}
 
private void closeOrOpenIssue() {
mProgress.setVisibility(View.VISIBLE);
if (mMilestone.getState().equals(Milestone.STATE_ACTIVE)) {
GitLabClient.instance().updateMilestoneStatus(mProject.getId(), mMilestone.getId(), Milestone.STATE_EVENT_CLOSE)
App.instance().getGitLab().updateMilestoneStatus(mProject.getId(), mMilestone.getId(), Milestone.STATE_EVENT_CLOSE)
.enqueue(mOpenCloseCallback);
} else {
GitLabClient.instance().updateMilestoneStatus(mProject.getId(), mMilestone.getId(), Milestone.STATE_EVENT_ACTIVATE)
App.instance().getGitLab().updateMilestoneStatus(mProject.getId(), mMilestone.getId(), Milestone.STATE_EVENT_ACTIVATE)
.enqueue(mOpenCloseCallback);
}
}
Loading
Loading
Loading
Loading
@@ -22,10 +22,9 @@ import android.widget.Toast;
 
import com.commit451.gitlab.App;
import com.commit451.gitlab.R;
import com.commit451.gitlab.adapter.SectionsPagerAdapter;
import com.commit451.gitlab.adapter.ProjectSectionsPagerAdapter;
import com.commit451.gitlab.animation.HideRunnable;
import com.commit451.easycallback.EasyCallback;
import com.commit451.gitlab.api.GitLabClient;
import com.commit451.gitlab.event.ProjectReloadEvent;
import com.commit451.gitlab.fragment.BaseFragment;
import com.commit451.gitlab.model.api.Branch;
Loading
Loading
@@ -210,14 +209,14 @@ public class ProjectActivity extends BaseActivity {
mProgress.setAlpha(0.0f);
mProgress.setVisibility(View.VISIBLE);
mProgress.animate().alpha(1.0f);
GitLabClient.instance().getProject(projectId).enqueue(mProjectCallback);
App.instance().getGitLab().getProject(projectId).enqueue(mProjectCallback);
}
 
private void loadBranches() {
mProgress.setAlpha(0.0f);
mProgress.setVisibility(View.VISIBLE);
mProgress.animate().alpha(1.0f);
GitLabClient.instance().getBranches(mProject.getId()).enqueue(mBranchesCallback);
App.instance().getGitLab().getBranches(mProject.getId()).enqueue(mBranchesCallback);
}
 
private void broadcastLoad() {
Loading
Loading
@@ -245,9 +244,9 @@ public class ProjectActivity extends BaseActivity {
}
 
private void setupTabs() {
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
ProjectSectionsPagerAdapter projectSectionsPagerAdapter = new ProjectSectionsPagerAdapter(this, getSupportFragmentManager());
 
mViewPager.setAdapter(sectionsPagerAdapter);
mViewPager.setAdapter(projectSectionsPagerAdapter);
mTabLayout.setupWithViewPager(mViewPager);
}
 
Loading
Loading
Loading
Loading
@@ -6,8 +6,8 @@ import android.net.Uri;
import android.os.Bundle;
import android.widget.Toast;
 
import com.commit451.gitlab.App;
import com.commit451.gitlab.R;
import com.commit451.gitlab.api.GitLabClient;
import com.commit451.gitlab.model.Account;
import com.commit451.gitlab.navigation.DeepLinker;
import com.commit451.gitlab.navigation.Navigator;
Loading
Loading
@@ -114,7 +114,7 @@ public class RoutingActivity extends Activity {
 
//okay so last thing, if the user has followed a link, but the user
//is not actually signed in, we want to direct them to signin
if (GitLabClient.getAccount() == null && Account.getAccounts(this).isEmpty()) {
if (App.instance().getAccount() == null && Account.getAccounts(this).isEmpty()) {
Navigator.navigateToLogin(this);
finish();
return;
Loading
Loading
Loading
Loading
@@ -21,8 +21,6 @@ import butterknife.OnClick;
*/
public class SettingsActivity extends BaseActivity {
 
private static final int REQUEST_COUNTRY = 1;
public static Intent newIntent(Context context) {
Intent intent = new Intent(context, SettingsActivity.class);
return intent;
Loading
Loading
@@ -34,7 +32,6 @@ public class SettingsActivity extends BaseActivity {
@BindView(R.id.text_launch_activity)
TextView mTextLaunchActivity;
 
@OnClick(R.id.root_launch_activity)
void onLaunchActivityClicked() {
new MaterialDialog.Builder(this)
Loading
Loading
Loading
Loading
@@ -16,8 +16,8 @@ import android.view.View;
import android.widget.ImageView;
 
import com.commit451.easel.Easel;
import com.commit451.gitlab.App;
import com.commit451.gitlab.R;
import com.commit451.gitlab.api.GitLabClient;
import com.commit451.gitlab.fragment.FeedFragment;
import com.commit451.gitlab.model.api.UserBasic;
import com.commit451.gitlab.transformation.PaletteTransformation;
Loading
Loading
@@ -68,7 +68,7 @@ public class UserActivity extends BaseActivity {
mToolbar.setTitle(mUser.getUsername());
Uri url = ImageUtil.getAvatarUrl(mUser, getResources().getDimensionPixelSize(R.dimen.user_header_image_size));
 
GitLabClient.getPicasso()
App.instance().getPicasso()
.load(url)
.transform(PaletteTransformation.instance())
.into(mBackdrop, new PaletteTransformation.PaletteCallback(mBackdrop) {
Loading
Loading
Loading
Loading
@@ -8,8 +8,8 @@ import android.view.ViewGroup;
import android.widget.PopupMenu;
 
import com.commit451.easel.Easel;
import com.commit451.gitlab.App;
import com.commit451.gitlab.R;
import com.commit451.gitlab.api.GitLabClient;
import com.commit451.gitlab.model.Account;
import com.commit451.gitlab.viewHolder.AccountFooterViewHolder;
import com.commit451.gitlab.viewHolder.AccountViewHolder;
Loading
Loading
@@ -91,7 +91,7 @@ public class AccountsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof AccountViewHolder) {
final Account account = getItemAtPosition(position);
((AccountViewHolder) holder).bind(account, account.equals(GitLabClient.getAccount()), mColorControlHighlight);
((AccountViewHolder) holder).bind(account, account.equals(App.instance().getAccount()), mColorControlHighlight);
holder.itemView.setTag(R.id.list_position, position);
((AccountViewHolder) holder).mPopupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
Loading
Loading
package com.commit451.gitlab.adapter;
 
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;
 
Loading
Loading
@@ -32,6 +33,11 @@ public class AddIssueLabelAdapter extends RecyclerView.Adapter<AddLabelViewHolde
notifyDataSetChanged();
}
 
public void addLabel(Label label) {
mValues.add(label);
notifyItemInserted(mValues.size()-1);
}
@Override
public AddLabelViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return AddLabelViewHolder.inflate(parent);
Loading
Loading
@@ -50,4 +56,22 @@ public class AddIssueLabelAdapter extends RecyclerView.Adapter<AddLabelViewHolde
private Label getEntry(int position) {
return mValues.get(position);
}
public boolean containsLabel(Label label) {
return mValues.contains(label);
}
@Nullable
public String getCommaSeperatedStringOfLabels() {
if (mValues.isEmpty()) {
return null;
}
String labels = "";
for (Label label : mValues) {
labels = labels + label.getName() + ",";
}
//Remove last ","
labels = labels.substring(0, labels.length()-1);
return labels;
}
}
Loading
Loading
@@ -13,15 +13,11 @@ import java.util.ArrayList;
import java.util.Collection;
 
/**
* Shows a projects members and a groups members
* Shows a bunch of labels
*/
public class LabelAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
 
private static final int TYPE_ITEM = 0;
private static final int TYPE_FOOTER = 1;
//TODO bring this back maybe?
private static final int FOOTER_COUNT = 0;
 
private Listener mListener;
 
Loading
Loading
@@ -36,13 +32,6 @@ public class LabelAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
}
};
 
private final View.OnClickListener mFooterClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onAddLabelClicked();
}
};
public LabelAdapter(Listener listener) {
mListener = listener;
mItems = new ArrayList<>();
Loading
Loading
@@ -61,8 +50,8 @@ public class LabelAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
}
 
public void addLabel(Label label) {
mItems.add(label);
notifyItemInserted(mItems.size());
mItems.add(0, label);
notifyItemInserted(0);
}
 
@Override
Loading
Loading
@@ -72,10 +61,6 @@ public class LabelAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
LabelViewHolder itemViewHolder = LabelViewHolder.inflate(parent);
itemViewHolder.itemView.setOnClickListener(mProjectMemberClickListener);
return itemViewHolder;
case TYPE_FOOTER:
ProjectMemberFooterViewHolder footerHolder = ProjectMemberFooterViewHolder.inflate(parent);
footerHolder.itemView.setOnClickListener(mFooterClickListener);
return footerHolder;
}
throw new IllegalStateException("No idea what to inflate with view type of " + viewType);
}
Loading
Loading
@@ -94,20 +79,15 @@ public class LabelAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
 
@Override
public int getItemCount() {
return mItems.size() + FOOTER_COUNT;
return mItems.size();
}
 
@Override
public int getItemViewType(int position) {
if (position == mItems.size()) {
return TYPE_FOOTER;
} else {
return TYPE_ITEM;
}
return TYPE_ITEM;
}
 
public interface Listener {
void onLabelClicked(Label label, LabelViewHolder viewHolder);
void onAddLabelClicked();
}
}
Loading
Loading
@@ -26,7 +26,7 @@ import timber.log.Timber;
/**
* Controls the sections that should be shown in a {@link com.commit451.gitlab.activity.ProjectActivity}
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public class ProjectSectionsPagerAdapter extends FragmentPagerAdapter {
 
private static final int PROJECT_POS = 0;
private static final int ACTIVITY_POS = 1;
Loading
Loading
@@ -43,7 +43,7 @@ public class SectionsPagerAdapter extends FragmentPagerAdapter {
private final String[] mTitles;
private final Set<Integer> mDisabledSections = new HashSet<>();
 
public SectionsPagerAdapter(ProjectActivity context, FragmentManager fm) {
public ProjectSectionsPagerAdapter(ProjectActivity context, FragmentManager fm) {
super(fm);
 
mProject = context.getProject();
Loading
Loading
@@ -66,7 +66,7 @@ public class SectionsPagerAdapter extends FragmentPagerAdapter {
Timber.d("Milestones are disabled");
mDisabledSections.add(MILESTONES_POS);
}
//TODO change this back when we do snippets
//TODO enable snippets when they are done
if (true){//!project.isSnippetsEnabled()) {
Timber.d("Snippets are disabled");
mDisabledSections.add(SNIPPETS_POS);
Loading
Loading
Loading
Loading
@@ -284,7 +284,8 @@ public interface GitLab {
@Field("title") String title,
@Field("description") String description,
@Field("assignee_id") @Nullable Long assigneeId,
@Field("milestone_id") @Nullable Long milestoneId);
@Field("milestone_id") @Nullable Long milestoneId,
@Field("labels") @Nullable String commaSeparatedLabelNames);
 
@PUT(API_VERSION + "/projects/{id}/issues/{issue_id}")
Call<Issue> updateIssue(@Path("id") long projectId,
Loading
Loading
@@ -292,7 +293,8 @@ public interface GitLab {
@Query("title") String title,
@Query("description") String description,
@Query("assignee_id") @Nullable Long assigneeId,
@Query("milestone_id") @Nullable Long milestoneId);
@Query("milestone_id") @Nullable Long milestoneId,
@Query("labels") @Nullable String commaSeparatedLabelNames);
 
@PUT(API_VERSION + "/projects/{id}/issues/{issue_id}")
Call<Issue> updateIssueStatus(@Path("id") long projectId,
Loading
Loading
@@ -363,7 +365,8 @@ public interface GitLab {
@POST(API_VERSION + "/projects/{id}/labels")
Call<Label> createLabel(@Path("id") long projectId,
@Query("name") String name,
@Query("color") String color);
@Query("color") String color,
@Query("description") @Nullable String description);
 
/**
* Delete the label by its name
Loading
Loading
package com.commit451.gitlab.api;
import com.commit451.gitlab.App;
import com.commit451.gitlab.model.Account;
import com.commit451.gitlab.provider.OkHttpClientProvider;
import com.commit451.gitlab.provider.SimpleXmlProvider;
import com.github.aurae.retrofit2.LoganSquareConverterFactory;
import com.jakewharton.picasso.OkHttp3Downloader;
import com.squareup.picasso.Picasso;
import java.util.List;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.simplexml.SimpleXmlConverterFactory;
/**
* Pulls all the GitLab stuff from the API
*/
public final class GitLabClient {
private static Account sAccount;
private static GitLab sGitLab;
private static GitLabRss sGitLabRss;
private static Picasso sPicasso;
public static void setAccount(Account account) {
sAccount = account;
sGitLab = null;
sGitLabRss = null;
sPicasso = null;
}
public static Account getAccount() {
return sAccount;
}
/**
* Create a GitLab instance with the current account passed.
* @param account the account to try and log in with
* @return the GitLab instance
*/
public static GitLab create(Account account) {
Retrofit restAdapter = new Retrofit.Builder()
.baseUrl(account.getServerUrl().toString())
.client(OkHttpClientProvider.createInstance(account))
.addConverterFactory(LoganSquareConverterFactory.create())
.build();
return restAdapter.create(GitLab.class);
}
public static GitLab instance() {
if (sGitLab == null) {
checkAccountSet();
sGitLab = create(sAccount);
}
return sGitLab;
}
public static GitLabRss rssInstance(Account account) {
Retrofit restAdapter = new Retrofit.Builder()
.baseUrl(account.getServerUrl().toString())
.client(OkHttpClientProvider.getInstance(account))
.addConverterFactory(SimpleXmlConverterFactory.create(SimpleXmlProvider.createPersister(account)))
.build();
return restAdapter.create(GitLabRss.class);
}
public static GitLabRss rssInstance() {
if (sGitLabRss == null) {
checkAccountSet();
sGitLabRss = rssInstance(sAccount);
}
return sGitLabRss;
}
public static Picasso getPicasso(Account account) {
OkHttpClient client = OkHttpClientProvider.getInstance(account);
return new Picasso.Builder(App.instance())
.downloader(new OkHttp3Downloader(client))
.build();
}
public static Picasso getPicasso() {
if (sPicasso == null) {
checkAccountSet();
sPicasso = getPicasso(sAccount);
}
return sPicasso;
}
private static void checkAccountSet() {
if (sAccount == null) {
List<Account> accounts = Account.getAccounts(App.instance());
if (accounts.isEmpty()) {
throw new IllegalStateException("No accounts found");
}
GitLabClient.setAccount(accounts.get(0));
}
}
private GitLabClient() {}
}
package com.commit451.gitlab.api;
import com.commit451.gitlab.model.Account;
import com.github.aurae.retrofit2.LoganSquareConverterFactory;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
/**
* Pulls all the GitLab stuff from the API
*/
public final class GitLabFactory {
/**
* Create a GitLab instance with the current account passed.
* @param account the account to try and log in with
* @return the GitLab instance
*/
public static GitLab create(Account account, OkHttpClient client) {
Retrofit restAdapter = new Retrofit.Builder()
.baseUrl(account.getServerUrl().toString())
.client(client)
.addConverterFactory(LoganSquareConverterFactory.create())
.build();
return restAdapter.create(GitLab.class);
}
}
package com.commit451.gitlab.api;
import com.commit451.gitlab.model.Account;
import com.commit451.gitlab.api.rss.SimpleXmlPersisterFactory;
import org.simpleframework.xml.core.Persister;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.simplexml.SimpleXmlConverterFactory;
/**
* Creates RSS instance for GitLab
*/
public class GitLabRssFactory {
public static GitLabRss create(Account account, OkHttpClient client) {
Persister persister = SimpleXmlPersisterFactory.createPersister(account);
Retrofit restAdapter = new Retrofit.Builder()
.baseUrl(account.getServerUrl().toString())
.client(client)
.addConverterFactory(SimpleXmlConverterFactory.create(persister))
.build();
return restAdapter.create(GitLabRss.class);
}
}
package com.commit451.gitlab.api;
import com.commit451.gitlab.model.Account;
import com.commit451.gitlab.ssl.CustomTrustManager;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
/**
* Creates an OkHttpClient with the needed defaults
*/
public final class OkHttpClientFactory {
/**
* Creates an {@link OkHttpClient} configured with the account configuration
*
* @param account the account
* @return a configured {@link okhttp3.OkHttpClient.Builder}
*/
public static OkHttpClient.Builder create(Account account) {
CustomTrustManager customTrustManager = null;
//Do we even need a custom trust manager?
if (account.getTrustedCertificate() != null
|| account.getTrustedHostname() != null
|| account.getPrivateKeyAlias() != null) {
customTrustManager = new CustomTrustManager();
customTrustManager.setTrustedCertificate(account.getTrustedCertificate());
customTrustManager.setTrustedHostname(account.getTrustedHostname());
customTrustManager.setPrivateKeyAlias(account.getPrivateKeyAlias());
}
OpenSignInAuthenticator authenticator = new OpenSignInAuthenticator(account);
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.authenticator(authenticator)
.proxyAuthenticator(authenticator)
.addInterceptor(new AuthenticationRequestInterceptor(account));
//Only apply these custom things when needed, since they slow down the init
if (customTrustManager != null && customTrustManager.getSSLSocketFactory() != null) {
clientBuilder.sslSocketFactory(customTrustManager.getSSLSocketFactory(), X509TrustManagerProvider.get());
}
if (customTrustManager != null && customTrustManager.getHostnameVerifier() != null) {
clientBuilder.hostnameVerifier(customTrustManager.getHostnameVerifier());
}
return clientBuilder;
}
}
package com.commit451.gitlab.api;
import com.commit451.gitlab.App;
import com.jakewharton.picasso.OkHttp3Downloader;
import com.squareup.picasso.Picasso;
import okhttp3.OkHttpClient;
/**
* Creates {@link com.squareup.picasso.Picasso} instances based on the account logged in to
*/
public class PicassoFactory {
public static Picasso createPicasso(OkHttpClient client) {
return new Picasso.Builder(App.instance())
.downloader(new OkHttp3Downloader(client))
.build();
}
}
package com.commit451.gitlab.api;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;
/**
* Gets the X509TrustManager on the system and caches it
*/
public class X509TrustManagerProvider {
private static X509TrustManager sX509TrustManager;
/**
* Get the static {@link X509TrustManager} for the system
* @return the static instance
*/
public static X509TrustManager get() {
if (sX509TrustManager == null) {
try {
init();
} catch (Exception any) {
//If they don't have X509 trust manager, they have bigger problems
throw new RuntimeException(any);
}
}
return sX509TrustManager;
}
private static void init() throws NoSuchAlgorithmException, KeyStoreException {
TrustManagerFactory factory = TrustManagerFactory.getInstance("X509");
factory.init((KeyStore) null);
TrustManager[] trustManagers = factory.getTrustManagers();
if (trustManagers != null) {
for (TrustManager trustManager : trustManagers) {
if (trustManager instanceof X509TrustManager) {
sX509TrustManager = (X509TrustManager) trustManager;
break;
}
}
}
}
}
package com.commit451.gitlab.api.exception;
import java.io.IOException;
import okhttp3.ResponseBody;
/**
* Represents an HTTP non 200 response from Retrofit
*/
public class HttpException extends Exception {
private int mCode;
private ResponseBody mErrorBody;
public HttpException(int code, ResponseBody errorBody) {
mCode = code;
mErrorBody = errorBody;
}
@Override
public String getMessage() {
try {
return mErrorBody.string();
} catch (IOException e) {
return e.toString();
}
}
public ResponseBody getResponseBody() {
return mErrorBody;
}
public int getCode() {
return mCode;
}
}
\ No newline at end of file
package com.commit451.gitlab.api.exception;
/**
* Represents that the body was null
*/
public class NullBodyException extends Exception{
@Override
public String getMessage() {
return "The Retrofit message was null";
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment