-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathRepository.java
More file actions
261 lines (218 loc) · 8.49 KB
/
Repository.java
File metadata and controls
261 lines (218 loc) · 8.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package com.bd.gitlab.tools;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.preference.PreferenceManager;
import android.util.DisplayMetrics;
import android.view.Surface;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ListAdapter;
import android.widget.ListView;
import com.bd.gitlab.adapter.DrawerAdapter;
import com.bd.gitlab.adapter.IssuesAdapter;
import com.bd.gitlab.adapter.UserAdapter;
import com.bd.gitlab.model.Branch;
import com.bd.gitlab.model.DiffLine;
import com.bd.gitlab.model.Group;
import com.bd.gitlab.model.Issue;
import com.bd.gitlab.model.Project;
import com.bd.gitlab.model.TreeItem;
import com.bd.gitlab.model.User;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import org.joda.time.format.ISODateTimeFormat;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Date;
import retrofit.RestAdapter;
import retrofit.converter.GsonConverter;
public class Repository extends Application {
public static ArrayList<Project> projects;
public static ArrayList<Branch> branches;
public static ArrayList<Group> groups;
public static ArrayList<User> users;
public static Project selectedProject;
public static Branch selectedBranch;
public static Issue selectedIssue;
public static TreeItem selectedFile;
public static User selectedUser;
public static DiffLine selectedCommit;
public static DiffLine newestCommit;
public static DrawerAdapter drawerAdapter;
public static IssuesAdapter issueAdapter;
public static UserAdapter userAdapter;
public static float displayWidth;
private static final String LOGGED_IN = "logged_in";
private static final String SERVER_URL = "server_url";
private static final String PRIVATE_TOKEN = "private_token";
private static final String LAST_PROJECT = "last_project";
private static final String LAST_BRANCH = "last_branch";
private static SharedPreferences preferences;
private static GitLabAPI service;
public static void init(Context context) {
preferences = PreferenceManager.getDefaultSharedPreferences(context);
projects = null;
branches = null;
groups = null;
users = null;
selectedProject = null;
selectedBranch = null;
selectedIssue = null;
selectedFile = null;
selectedUser = null;
newestCommit = null;
drawerAdapter = null;
issueAdapter = null;
userAdapter = null;
}
public static GitLabAPI getService() {
if(getServerUrl().length() < 1) {
Repository.setLoggedIn(false);
Repository.setPrivateToken("");
Repository.setServerUrl("");
service = null;
return null;
}
if(service == null) {
// Configure Gson to handle dates correctly
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
@Override
public Date deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException {
return ISODateTimeFormat.dateTimeParser().parseDateTime(json.getAsString()).toDate();
}
});
Gson gson = gsonBuilder.create();
RestAdapter restAdapter = new RestAdapter.Builder().setRequestInterceptor(new GitLabInterceptor()).setConverter(new GsonConverter(gson)).setEndpoint(getServerUrl() + "/api/v3").build();
service = restAdapter.create(GitLabAPI.class);
}
return service;
}
public static boolean isLoggedIn() {
return preferences.getBoolean(LOGGED_IN, false);
}
public static void setLoggedIn(boolean loggedIn) {
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(LOGGED_IN, loggedIn);
editor.commit();
}
public static void setServerUrl(String serverUrl) {
SharedPreferences.Editor editor = preferences.edit();
editor.putString(SERVER_URL, serverUrl);
editor.commit();
service = null;
}
public static String getServerUrl() {
return preferences.getString(SERVER_URL, "");
}
public static void setPrivateToken(String privateToken) {
SharedPreferences.Editor editor = preferences.edit();
editor.putString(PRIVATE_TOKEN, privateToken);
editor.commit();
}
public static String getPrivateToken() {
return preferences.getString(PRIVATE_TOKEN, "");
}
public static void setLastProject(String lastProject) {
SharedPreferences.Editor editor = preferences.edit();
editor.putString(LAST_PROJECT, lastProject);
editor.commit();
}
public static String getLastProject() {
return preferences.getString(LAST_PROJECT, "");
}
public static void setLastBranch(String lastBranch) {
SharedPreferences.Editor editor = preferences.edit();
editor.putString(LAST_BRANCH, lastBranch);
editor.commit();
}
public static String getLastBranch() {
return preferences.getString(LAST_BRANCH, "");
}
public static void setListViewSize(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if(listAdapter == null)
return;
int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
for(int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
if(listItem instanceof ViewGroup)
listItem.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
public static int getScreenOrientation(WindowManager windowManager) {
int rotation = windowManager.getDefaultDisplay().getRotation();
DisplayMetrics dm = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
int orientation;
// if the device's natural orientation is portrait:
if ((rotation == Surface.ROTATION_0
|| rotation == Surface.ROTATION_180) && height > width ||
(rotation == Surface.ROTATION_90
|| rotation == Surface.ROTATION_270) && width > height) {
switch(rotation) {
case Surface.ROTATION_0:
orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
break;
case Surface.ROTATION_90:
orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
break;
case Surface.ROTATION_180:
orientation =
ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
break;
case Surface.ROTATION_270:
orientation =
ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
break;
default:
orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
break;
}
}
// if the device's natural orientation is landscape or if the device
// is square:
else {
switch(rotation) {
case Surface.ROTATION_0:
orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
break;
case Surface.ROTATION_90:
orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
break;
case Surface.ROTATION_180:
orientation =
ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
break;
case Surface.ROTATION_270:
orientation =
ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
break;
default:
orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
break;
}
}
return orientation;
}
public static void resetService() {
service = null;
}
}