Download contacts result

pull/213/head
M66B 2 years ago
parent d04ef89006
commit 66365b0dd4

@ -54,7 +54,6 @@ import android.widget.CompoundButton;
import android.widget.ImageButton; import android.widget.ImageButton;
import android.widget.ScrollView; import android.widget.ScrollView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@ -71,8 +70,9 @@ import androidx.lifecycle.Observer;
import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import com.google.android.material.snackbar.Snackbar;
import net.openid.appauth.AppAuthConfiguration; import net.openid.appauth.AppAuthConfiguration;
import net.openid.appauth.AuthState;
import net.openid.appauth.AuthorizationException; import net.openid.appauth.AuthorizationException;
import net.openid.appauth.AuthorizationRequest; import net.openid.appauth.AuthorizationRequest;
import net.openid.appauth.AuthorizationResponse; import net.openid.appauth.AuthorizationResponse;
@ -86,16 +86,8 @@ import net.openid.appauth.ResponseTypeValues;
import net.openid.appauth.TokenRequest; import net.openid.appauth.TokenRequest;
import net.openid.appauth.TokenResponse; import net.openid.appauth.TokenResponse;
import org.json.JSONArray; import java.text.NumberFormat;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List; import java.util.List;
public class FragmentSetup extends FragmentBase implements SharedPreferences.OnSharedPreferenceChangeListener { public class FragmentSetup extends FragmentBase implements SharedPreferences.OnSharedPreferenceChangeListener {
@ -1369,21 +1361,32 @@ public class FragmentSetup extends FragmentBase implements SharedPreferences.OnS
args.putLong("account", account); args.putLong("account", account);
args.putString("accessToken", access.accessToken); args.putString("accessToken", access.accessToken);
new SimpleTask<Void>() { new SimpleTask<Integer>() {
@Override @Override
protected Void onExecute(Context context, Bundle args) throws Throwable { protected Integer onExecute(Context context, Bundle args) throws Throwable {
long account = args.getLong("account"); long account = args.getLong("account");
String accessToken = args.getString("accessToken"); String accessToken = args.getString("accessToken");
MicrosoftGraph.downloadContacts(context, account, accessToken); return MicrosoftGraph.downloadContacts(context, account, accessToken);
return null;
} }
@Override @Override
protected void onExecuted(Bundle args, Void data) { protected void onExecuted(Bundle args, @NonNull Integer count) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.edit().putBoolean("suggest_sent", true).apply(); prefs.edit().putBoolean("suggest_sent", true).apply();
ToastEx.makeText(context, R.string.title_completed, Toast.LENGTH_LONG).show();
NumberFormat NF = NumberFormat.getInstance();
String msg = getString(R.string.title_setup_import_graph_result, NF.format(count));
final Snackbar snackbar = Snackbar.make(view, msg, Snackbar.LENGTH_INDEFINITE)
.setGestureInsetBottomIgnored(true);
snackbar.setAction(android.R.string.ok, new View.OnClickListener() {
@Override
public void onClick(View view) {
snackbar.dismiss();
}
});
snackbar.show();
} }
@Override @Override

@ -124,7 +124,8 @@ public class MicrosoftGraph {
} }
} }
static void downloadContacts(Context context, long account, String accessToken) throws IOException, JSONException { static int downloadContacts(Context context, long account, String accessToken) throws IOException, JSONException {
int count = 0;
DB db = DB.getInstance(context); DB db = DB.getInstance(context);
// https://learn.microsoft.com/en-us/graph/api/user-list-contacts?view=graph-rest-1.0&tabs=http // https://learn.microsoft.com/en-us/graph/api/user-list-contacts?view=graph-rest-1.0&tabs=http
@ -168,6 +169,7 @@ public class MicrosoftGraph {
contact.first_contacted = new Date().getTime(); contact.first_contacted = new Date().getTime();
contact.last_contacted = contact.first_contacted; contact.last_contacted = contact.first_contacted;
db.contact().insertContact(contact); db.contact().insertContact(contact);
count++;
} }
} }
} }
@ -187,5 +189,7 @@ public class MicrosoftGraph {
} finally { } finally {
connection.disconnect(); connection.disconnect();
} }
return count;
} }
} }

@ -262,6 +262,7 @@
<string name="title_setup_permission_why">Which permissions are needed and why?</string> <string name="title_setup_permission_why">Which permissions are needed and why?</string>
<string name="title_setup_permission_import_contacts">How can I import contacts?</string> <string name="title_setup_permission_import_contacts">How can I import contacts?</string>
<string name="title_setup_import_graph_contact">Outlook contacts</string> <string name="title_setup_import_graph_contact">Outlook contacts</string>
<string name="title_setup_import_graph_result">New contacts: %1$s</string>
<string name="title_setup_doze">Disable battery optimizations</string> <string name="title_setup_doze">Disable battery optimizations</string>
<string name="title_setup_doze_remark">To send and receive email reliably in the background</string> <string name="title_setup_doze_remark">To send and receive email reliably in the background</string>
<string name="title_setup_doze_explanation">Battery optimizations should be disabled for the app to ensure reliable sending and receiving of messages in the background</string> <string name="title_setup_doze_explanation">Battery optimizations should be disabled for the app to ensure reliable sending and receiving of messages in the background</string>

Loading…
Cancel
Save