Skip to content

Commit

Permalink
Merge pull request #240 from jlewis13/directory_reset
Browse files Browse the repository at this point in the history
Directory reset
  • Loading branch information
jlewis13 authored Dec 19, 2017
2 parents e594ddd + 703e6b6 commit ce425e8
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 7 deletions.
4 changes: 4 additions & 0 deletions res/menu/new_conversation_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
android:id="@+id/menu_refresh"
app:showAsAction="never" />

<item android:title="@string/new_conversation_activity__reset"
android:id="@+id/menu_reset"
app:showAsAction="never" />

<item android:title="@string/text_secure_normal__menu_new_group"
android:id="@+id/menu_new_group" android:visible="false" />

Expand Down
1 change: 1 addition & 0 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,7 @@

<!-- new_conversation_activity -->
<string name="new_conversation_activity__refresh">Refresh</string>
<string name="new_conversation_activity__reset">Reset Directory</string>
<!-- redphone_audio_popup_menu -->
<string name="redphone_audio_popup_menu__handset">Handset</string>
<string name="redphone_audio_popup_menu__headset">Headset</string>
Expand Down
8 changes: 6 additions & 2 deletions src/io/forsta/ccsm/api/CcsmApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ public static JSONObject forstaRefreshToken(Context context) {
}

public static void syncForstaContacts(Context context) {
syncForstaContacts(context, false);
}

public static void syncForstaContacts(Context context, boolean removeInvalidUsers) {
try {
ForstaOrg org = ForstaOrg.getLocalForstaOrg(context);
if (org.getSlug().equals("public") || org.getSlug().equals("forsta")) {
Expand All @@ -164,7 +168,7 @@ public static void syncForstaContacts(Context context) {
threadContacts.add(user);
}
}
syncForstaContactsDb(context, threadContacts, false);
syncForstaContactsDb(context, threadContacts, removeInvalidUsers);
} else {
JSONObject orgUsers = getOrgUsers(context);
List<ForstaUser> orgContacts = parseUsers(context, orgUsers);
Expand All @@ -179,7 +183,7 @@ public static void syncForstaContacts(Context context) {
List<ForstaUser> threadContacts = CcsmApi.parseUsers(context, threadUsers);
orgContacts.addAll(threadContacts);
}
syncForstaContactsDb(context, orgContacts, false);
syncForstaContactsDb(context, orgContacts, removeInvalidUsers);
CcsmApi.syncOrgTags(context);
}
ForstaPreferences.setForstaContactSync(context, new Date().getTime());
Expand Down
1 change: 1 addition & 0 deletions src/io/forsta/ccsm/database/ContactDb.java
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ public void updateUsers(List<ForstaUser> users, boolean removeExisting) {
db.endTransaction();
}
if (removeExisting) {
Log.w(TAG, "Reseting directory. Removing " + uids.size() + " entries.");
db.beginTransaction();
try {
// Now remove entries that are no longer valid.
Expand Down
17 changes: 14 additions & 3 deletions src/io/forsta/securesms/ContactSelectionActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ protected void updateToggleBar() {

@Override
public void onRefresh() {
new RefreshDirectoryTask(this).execute(getApplicationContext());
new RefreshDirectoryTask(this, false).execute(ContactSelectionActivity.this);
}

public void resetDirectory() {
new RefreshDirectoryTask(this, true).execute(ContactSelectionActivity.this);
}

@Override
Expand All @@ -159,17 +163,24 @@ private class RefreshDirectoryTask extends AsyncTask<Context, Void, Void> {

private final WeakReference<ContactSelectionActivity> activity;
private final MasterSecret masterSecret;
private final boolean reset;

private RefreshDirectoryTask(ContactSelectionActivity activity) {
private RefreshDirectoryTask(ContactSelectionActivity activity, boolean reset) {
this.activity = new WeakReference<>(activity);
this.masterSecret = activity.masterSecret;
this.reset = reset;
}


@Override
protected Void doInBackground(Context... params) {
try {
DirectoryHelper.refreshDirectory(params[0], masterSecret);
if (reset) {
DirectoryHelper.resetDirectory(params[0]);
} else {
DirectoryHelper.refreshDirectory(params[0], masterSecret);
}

} catch (IOException e) {
e.printStackTrace();
}
Expand Down
6 changes: 6 additions & 0 deletions src/io/forsta/securesms/NewConversationActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home: super.onBackPressed(); return true;
case R.id.menu_refresh: handleManualRefresh(); return true;
case R.id.menu_reset: handleReset(); return true;
case R.id.menu_new_group: handleCreateGroup(); return true;
}

Expand All @@ -294,6 +295,11 @@ private void handleManualRefresh() {
onRefresh();
}

private void handleReset() {
contactsFragment.setRefreshing(true);
resetDirectory();
}

private void handleCreateGroup() {
startActivity(new Intent(this, GroupCreateActivity.class));
}
Expand Down
12 changes: 10 additions & 2 deletions src/io/forsta/securesms/util/DirectoryHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,20 @@
public class DirectoryHelper {
private static final String TAG = DirectoryHelper.class.getSimpleName();

public static void resetDirectory(Context context) throws IOException {
refreshDirectory(context, TextSecureCommunicationFactory.createManager(context), TextSecurePreferences.getLocalNumber(context), true);
}

public static void refreshDirectory(@NonNull Context context, @Nullable MasterSecret masterSecret) throws IOException {
refreshDirectory(context, TextSecureCommunicationFactory.createManager(context), TextSecurePreferences.getLocalNumber(context));
refreshDirectory(context, TextSecureCommunicationFactory.createManager(context), TextSecurePreferences.getLocalNumber(context), false);
}

public static void refreshDirectory(@NonNull Context context, @NonNull ForstaServiceAccountManager accountManager, @NonNull String localNumber) throws IOException {
CcsmApi.syncForstaContacts(context);
refreshDirectory(context, accountManager, localNumber, false);
}

public static void refreshDirectory(@NonNull Context context, @NonNull ForstaServiceAccountManager accountManager, @NonNull String localNumber, boolean resetDirectory) throws IOException {
CcsmApi.syncForstaContacts(context, resetDirectory);
ContactDb contactsDb = DbFactory.getContactDb(context);
Set<String> eligibleContactAddresses = contactsDb.getAddresses();
// Two devices had crashes because of a null UID value in the contactsDb
Expand Down

0 comments on commit ce425e8

Please sign in to comment.