Skip to content

Commit

Permalink
[#166]Fetch projects one per call at new API /rest/v1/project_up
Browse files Browse the repository at this point in the history
[#163]Allow login for user belonging to 0, 1 or more organisations
  • Loading branch information
stellanl committed Apr 2, 2015
1 parent 34b6c09 commit c5fa25d
Show file tree
Hide file tree
Showing 12 changed files with 496 additions and 149 deletions.
28 changes: 8 additions & 20 deletions android/AkvoRSR/res/layout/activity_project_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -150,26 +150,14 @@
android:visibility="gone"
/>

<!-- Refresh button
<RelativeLayout
<TextView
android:id="@+id/unemployed_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/btn_border"
android:padding="5dip" >
<Button
android:id="@+id/btn_refresh_projects"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/btn_face"
android:drawableLeft="@drawable/ic_menu_refresh"
android:padding="5dip"
android:text="@string/btncaption_refresh"
android:textColor="@color/rsr_purple"
android:textSize="18sp"
android:typeface="sans" />
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="@string/label_unemployed_proj_list"
android:visibility="gone"
/>

</RelativeLayout>
-->
</LinearLayout>
1 change: 1 addition & 0 deletions android/AkvoRSR/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<string name="label_photo_credit">Photo: %s</string>
<string name="label_empty_proj_list">Nothing to show with this filter.</string>
<string name="label_first_proj_list">Nothing fetched yet. Use the refresh button to see your projects.\n\nIf you have a low-bandwidth connection you should turn on delayed picture fetching in settings first.</string>
<string name="label_unemployed_proj_list">You are not connected to any projects.\n\nYou must use the web inteface to log in and request to be added to one first.</string>
<string name="label_proj_list">Your Projects:</string>
<string name="label_update_list">Updates:</string>
<string name="label_refresh_progress">Refresh progress</string>
Expand Down
25 changes: 22 additions & 3 deletions android/AkvoRSR/src/org/akvo/rsr/up/ProjectListActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2012-2014 Stichting Akvo (Akvo Foundation)
* Copyright (C) 2012-2015 Stichting Akvo (Akvo Foundation)
*
* This file is part of Akvo RSR.
*
Expand Down Expand Up @@ -66,12 +66,18 @@ public class ProjectListActivity extends ActionBarActivity {
private ListView mList;
private TextView mEmptyText;
private TextView mFirstTimeText;
private TextView mUnemployedText;
private BroadcastReceiver broadRec;
private Button searchButton;

private boolean mEmployed; //False if user is not employed with any organisation

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//employment can for now only change at login, so assign it for life of activity
mEmployed = SettingsUtil.getAuthUser(this).getOrgIds().size() > 0;

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_project_list);

projCountLabel = (TextView) findViewById(R.id.projcountlabel);
Expand All @@ -83,6 +89,7 @@ protected void onCreate(Bundle savedInstanceState) {
mList = (ListView) findViewById(R.id.list_projects);
mEmptyText = (TextView) findViewById(R.id.list_empty_text);
mFirstTimeText = (TextView) findViewById(R.id.first_time_text);
mUnemployedText = (TextView) findViewById(R.id.unemployed_text);
mList.setOnItemClickListener(new OnItemClickListener() {

@Override
Expand Down Expand Up @@ -247,17 +254,25 @@ private void getData() {
}
if (count == 0) { //no records, but why?
mList.setVisibility(View.GONE);
if (!mEmployed) {
mEmptyText.setVisibility(View.GONE);
mFirstTimeText.setVisibility(View.GONE);
mUnemployedText.setVisibility(View.VISIBLE);
} else
if (searchString == null || searchString.length() == 0) { //must be empty DB
mEmptyText.setVisibility(View.GONE);
mFirstTimeText.setVisibility(View.VISIBLE);
mUnemployedText.setVisibility(View.GONE);
} else { //too filtered
mEmptyText.setVisibility(View.VISIBLE);
mFirstTimeText.setVisibility(View.GONE);
mUnemployedText.setVisibility(View.GONE);
}
} else {
mList.setVisibility(View.VISIBLE);
mEmptyText.setVisibility(View.GONE);
mFirstTimeText.setVisibility(View.GONE);
mUnemployedText.setVisibility(View.GONE);
}
//Populate list view
ProjectListCursorAdapter projects = new ProjectListCursorAdapter(this, dataCursor);
Expand All @@ -270,10 +285,14 @@ private void getData() {
* starts the service fetching new project data
*/
private void startGetProjectsService() {
if (!mEmployed) { //TODO should disable menu choice instead
return; //fetch would fail
}

if (GetProjectDataService.isRunning(this)) { //TODO should disable menu choice instead
return; //only one at a time
}
//TODO: disable menu choice
//start a service
Intent i = new Intent(this, GetProjectDataService.class);
Expand Down
62 changes: 48 additions & 14 deletions android/AkvoRSR/src/org/akvo/rsr/up/domain/User.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2012-2013 Stichting Akvo (Akvo Foundation)
* Copyright (C) 2012-2015 Stichting Akvo (Akvo Foundation)
*
* This file is part of Akvo RSR.
*
Expand Down Expand Up @@ -28,10 +28,12 @@ public class User {
private String email;
private String apiKey;
private String orgId;
private Set<String> publishedProjects;
private Set<String> mOrgIds;
private Set<String> publishedProjects;

public User() {
publishedProjects = new HashSet<String>(10);
mOrgIds = new HashSet<String>(2);
publishedProjects = new HashSet<String>(10);
}

public String getId() {
Expand Down Expand Up @@ -90,15 +92,47 @@ public void setApiKey(String summary) {
this.apiKey = summary;
}

public Set<String> getPublishedProjects() {
return publishedProjects;
}

public void addPublishedProject(String id) {
this.publishedProjects.add(id);
}

public void clearPublishedProjects() {
this.publishedProjects.clear();
}
public Set<String> getPublishedProjIds() {
return publishedProjects;
}

public String getPublishedProjIdsString() {
String projlist = "";
for (String id : publishedProjects) {
projlist += id + ",";
}
if (projlist.length() > 0)
projlist = projlist.substring(0, projlist.length()-1);
return projlist;
}

public void addPublishedProjId(String id) {
this.publishedProjects.add(id);
}

public void clearPublishedProjIds() {
this.publishedProjects.clear();
}

public Set<String> getOrgIds() {
return mOrgIds;
}

public String getOrgIdsString() {
String orglist = "";
for (String id : mOrgIds) {
orglist += id + ",";
}
if (orglist.length() > 0)
orglist=orglist.substring(0, orglist.length()-1);
return orglist;
}

public void addOrgId(String id) {
this.mOrgIds.add(id);
}

public void clearOrgIds() {
this.mOrgIds.clear();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2012-2014 Stichting Akvo (Akvo Foundation)
* Copyright (C) 2012-2015 Stichting Akvo (Akvo Foundation)
*
* This file is part of Akvo RSR.
*
Expand Down Expand Up @@ -30,12 +30,9 @@
import org.akvo.rsr.up.util.FileUtil;
import org.akvo.rsr.up.util.SettingsUtil;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

Expand All @@ -55,18 +52,14 @@ public GetProjectDataService() {

public static boolean isRunning(Context context) {
return mRunning;
/* this solution uses an interface documented as intended for debug use
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (GetProjectDataService.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
*/
}



/**
* Fetch data from server.
* TODO: Send the object type as a string in the broadcastProgress call so we can have that displayed as part of the progress bar.
*/
@Override
protected void onHandleIntent(Intent intent) {
mRunning = true;
Expand All @@ -77,38 +70,32 @@ protected void onHandleIntent(Intent intent) {
String host = SettingsUtil.host(this);

ad.open();
User user = SettingsUtil.getAuthUser(this);
try {
try {
dl.fetchProjectList(this,
new URL(SettingsUtil.host(this) +
String.format(ConstantUtil.FETCH_PROJ_URL_PATTERN,
SettingsUtil.Read(this, "authorized_orgid"))));
broadcastProgress(0, 50, 100);
int i = 0;
int projects = user.getPublishedProjIds().size();
//Iterate over projects instead of using a complex query URL, since it can take so long that the proxy times out
for (String id : user.getPublishedProjIds()) {
dl.fetchProject(this,
new URL(SettingsUtil.host(this) +
String.format(ConstantUtil.FETCH_PROJ_URL_PATTERN,id)));
broadcastProgress(0, i++, projects);
}
if (mFetchCountries) {
// TODO: rarely changes, so only fetch countries if we never
// did that
// TODO: rarely changes, so only fetch countries if we never did that
dl.fetchCountryList(this, new URL(SettingsUtil.host(this) +
String.format(ConstantUtil.FETCH_COUNTRIES_URL)));
}
broadcastProgress(0, 100, 100);

if (mFetchUpdates) {
// We only get published projects from that URL,
// so we need to iterate on them and get corresponding updates
Cursor c = ad.listAllProjects();
try {
int i = 0;
while (c.moveToNext()) {
i++;
String projId = c.getString(c.getColumnIndex(RsrDbAdapter.PK_ID_COL));
dl.fetchUpdateListRestApi(this, //TODO: use _extra for fewer fetches, as country and user is included
new URL(host + String.format(ConstantUtil.FETCH_UPDATE_URL_PATTERN, projId))
);
broadcastProgress(1, i, c.getCount());
}
} finally {
if (c != null)
c.close();
int j = 0;
for (String projId : user.getPublishedProjIds()) {
dl.fetchUpdateListRestApi(this, //TODO: use _extra for fewer fetches, as country and user data is included
new URL(host + String.format(ConstantUtil.FETCH_UPDATE_URL_PATTERN, projId))
);
broadcastProgress(1, j++, projects);
}
}

Expand All @@ -120,13 +107,12 @@ protected void onHandleIntent(Intent intent) {
errMsg = getResources().getString(R.string.errmsg_update_fetch_failed) + e.getMessage();
}

if (mFetchUsers) {
if (mFetchUsers) { //Remove this once we use the _extra update API
// Fetch missing user data for authors of the updates.
// This API requires authorization
User user = SettingsUtil.getAuthUser(this);
String key = String.format(Locale.US, ConstantUtil.API_KEY_PATTERN,
user.getApiKey(), user.getUsername());
int j = 0;
// int k = 0;
List<String> orgIds = ad.getMissingUsersList();
for (String id : orgIds) {
try {
Expand All @@ -139,7 +125,7 @@ protected void onHandleIntent(Intent intent) {
key),
id
);
j++;
// k++;
} catch (FileNotFoundException e) {
// possibly because user is no longer active
Log.w(TAG, "Cannot find user:" + id);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2012-2014 Stichting Akvo (Akvo Foundation)
* Copyright (C) 2012-2015 Stichting Akvo (Akvo Foundation)
*
* This file is part of Akvo RSR.
*
Expand Down Expand Up @@ -57,7 +57,7 @@ protected void onHandleIntent(Intent intent) {
//use project list to set projects visible
RsrDbAdapter dba = new RsrDbAdapter(this);
dba.open();
dba.setVisibleProjects(user.getPublishedProjects());
dba.setVisibleProjects(user.getPublishedProjIds());
dba.close();

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright (C) 2012-2015 Stichting Akvo (Akvo Foundation)
*
* This file is part of Akvo RSR.
*
* Akvo RSR is free software: you can redistribute it and modify it under the terms of
* the GNU Affero General Public License (AGPL) as published by the Free Software Foundation,
* either version 3 of the License or any later version.
*
* Akvo RSR 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 Affero General Public License included with this program for more details.
*
* The full license text can also be seen at <http://www.gnu.org/licenses/agpl.html>.
*/

package org.akvo.rsr.up.service;

import org.akvo.rsr.up.domain.User;
Expand Down
11 changes: 5 additions & 6 deletions android/AkvoRSR/src/org/akvo/rsr/up/util/ConstantUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2012-2014 Stichting Akvo (Akvo Foundation)
* Copyright (C) 2012-2015 Stichting Akvo (Akvo Foundation)
*
* This file is part of Akvo RSR.
*
Expand Down Expand Up @@ -33,12 +33,10 @@ public class ConstantUtil {
public static final String PWD_URL = "/sign_in/";
public static final String AUTH_URL = "/auth/token/";
public static final String API_KEY_PATTERN = "&api_key=%s&username=%s";
// public static final String POST_UPDATE_URL = "/api/v1/project_update/?format=xml";
public static final String POST_UPDATE_URL = "/rest/v1/project_update/?format=xml";
public static final String FETCH_UPDATE_URL_PATTERN = "/rest/v1/project_update/?format=xml&limit=1000&project=%s"; // /api/v1/project_update/?format=xml&limit=0&project=
// public static final String VERIFY_UPDATE_PATTERN = "/api/v1/project_update/?format=xml&uuid=%s&limit=2";
public static final String FETCH_UPDATE_URL_PATTERN = "/rest/v1/project_update/?format=xml&limit=1000&project=%s";
public static final String VERIFY_UPDATE_PATTERN = "/rest/v1/project_update/?format=xml&uuid=%s&limit=2";
public static final String FETCH_PROJ_URL_PATTERN = "/api/v1/project/?format=xml&limit=0&partnerships__organisation=%s";
public static final String FETCH_PROJ_URL_PATTERN = "/rest/v1/project_up/%s/?format=xml&image_thumb_name=up&width=100"; //ask for thumbnail size
public static final String FETCH_COUNTRIES_URL = "/api/v1/country/?format=xml&limit=0";
public static final String FETCH_PROJ_COUNT_URL = "/api/v1/project/?format=xml&limit=0&partnerships__organisation=%s";
public static final String PROJECT_PATH_PATTERN = "/api/v1/project/%s/";
Expand Down Expand Up @@ -93,7 +91,8 @@ public class ConstantUtil {
public static final String AUTH_USERNAME_KEY = "authorized_username";
public static final String AUTH_APIKEY_KEY = "authorized_apikey";
public static final String AUTH_USERID_KEY = "authorized_userid";
public static final String AUTH_ORGID_KEY = "authorized_orgid";
public static final String AUTH_ORGID_KEY = "authorized_orgid";
public static final String AUTH_PROJID_KEY = "authorized_projid";
public static final String LOCAL_ID_KEY = "next_local_id";

/**
Expand Down
Loading

0 comments on commit c5fa25d

Please sign in to comment.