Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shorten Firebase Dynamic Links #719

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.gdg.frisbee.android.api;

import org.gdg.frisbee.android.api.model.FirebaseDynamicLinksRequest;
import org.gdg.frisbee.android.api.model.FirebaseDynamicLinksResponse;

import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.POST;
import retrofit2.http.Query;

public interface FirebaseDynamicLinks {

@POST("/v1/shortLinks")
Call<FirebaseDynamicLinksResponse> shortenUrl(@Query("key") String apiKey,
@Body FirebaseDynamicLinksRequest firebaseDynamicLinksRequest);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.gdg.frisbee.android.api;

import com.google.gson.FieldNamingPolicy;

import org.gdg.frisbee.android.utils.Utils;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public final class FirebaseDynamicLinksFactory {
private static final String BASE_URL = "https://firebasedynamiclinks.googleapis.com";

private FirebaseDynamicLinksFactory() {
}

private static Retrofit provideRestAdapter(OkHttpClient okHttpClient) {
return new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(okHttpClient)
.addConverterFactory(
GsonConverterFactory.create(Utils.getGson(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES))
)
.build();
}

public static FirebaseDynamicLinks provideFirebaseDynamicLinksApi(OkHttpClient okHttpClient) {
return provideRestAdapter(okHttpClient).create(FirebaseDynamicLinks.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.gdg.frisbee.android.api.model;

public class FirebaseDynamicLinksRequest {
private String longDynamicLink;
private Suffix suffix;

public FirebaseDynamicLinksRequest(String longDynamicLink, Suffix suffix) {
this.longDynamicLink = longDynamicLink;
this.suffix = suffix;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.gdg.frisbee.android.api.model;

public class FirebaseDynamicLinksResponse {
private final String shortLink;
private final String previewLink;

public FirebaseDynamicLinksResponse(String shortLink, String previewLink) {
this.shortLink = shortLink;
this.previewLink = previewLink;
}

public String getShortLink() {
return shortLink;
}

public String getPreviewLink() {
return previewLink;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.gdg.frisbee.android.api.model;

public class Suffix {
private final String option = "SHORT";
}
10 changes: 10 additions & 0 deletions app/src/main/java/org/gdg/frisbee/android/app/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

import org.gdg.frisbee.android.BuildConfig;
import org.gdg.frisbee.android.R;
import org.gdg.frisbee.android.api.FirebaseDynamicLinks;
import org.gdg.frisbee.android.api.FirebaseDynamicLinksFactory;
import org.gdg.frisbee.android.api.GdeDirectory;
import org.gdg.frisbee.android.api.GdeDirectoryFactory;
import org.gdg.frisbee.android.api.GdgXHub;
Expand Down Expand Up @@ -78,6 +80,7 @@ public class App extends BaseApp implements LocationListener {
private OrganizerChecker mOrganizerChecker;
private List<TaggedEventSeries> mTaggedEventSeriesList;
private RefWatcher refWatcher;
private FirebaseDynamicLinks firebaseDynamicLinks;

public static App from(Context context) {
return (App) context.getApplicationContext();
Expand Down Expand Up @@ -296,6 +299,13 @@ public PlusApi getPlusApi() {
return plusApi;
}

public FirebaseDynamicLinks getFirebaseDynamicLinks() {
if (firebaseDynamicLinks == null) {
firebaseDynamicLinks = FirebaseDynamicLinksFactory.provideFirebaseDynamicLinksApi(okHttpClient);
}
return firebaseDynamicLinks;
}

public RefWatcher getRefWatcher() {
return refWatcher;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package org.gdg.frisbee.android.onboarding;

import android.app.ProgressDialog;
import android.support.v4.app.ShareCompat;

import org.gdg.frisbee.android.BuildConfig;
import org.gdg.frisbee.android.R;
import org.gdg.frisbee.android.api.Callback;
import org.gdg.frisbee.android.api.FirebaseDynamicLinks;
import org.gdg.frisbee.android.api.model.FirebaseDynamicLinksRequest;
import org.gdg.frisbee.android.api.model.FirebaseDynamicLinksResponse;
import org.gdg.frisbee.android.api.model.Suffix;
import org.gdg.frisbee.android.app.App;
import org.gdg.frisbee.android.common.GdgActivity;
import org.gdg.frisbee.android.utils.PlusUtils;

Expand All @@ -16,31 +23,36 @@ public class AppInviteLinkGenerator {

private final String dynamicLinkDomain;
private final String deepLinkBaseUrl;
private GdgActivity activity;
private ProgressDialog shareProgressDialog;

private AppInviteLinkGenerator(String dynamicLinkDomain, String deepLinkBaseUrl, GdgActivity activity) {
this.dynamicLinkDomain = dynamicLinkDomain;
this.deepLinkBaseUrl = deepLinkBaseUrl;
this.activity = activity;
}

static String extractSender(HttpUrl httpUrl) {
return httpUrl.queryParameter(SENDER);
}

public static AppInviteLinkGenerator create() {
return new AppInviteLinkGenerator("https://fmec6.app.goo.gl/", "https://gdg.events/");
}

private AppInviteLinkGenerator(String dynamicLinkDomain, String deepLinkBaseUrl) {
this.dynamicLinkDomain = dynamicLinkDomain;
this.deepLinkBaseUrl = deepLinkBaseUrl;
public static AppInviteLinkGenerator create(GdgActivity activity) {
return new AppInviteLinkGenerator("https://fmec6.app.goo.gl/", "https://gdg.events/", activity);
}

public static void shareAppInviteLink(GdgActivity activity) {
AppInviteLinkGenerator linkGenerator = create();
AppInviteLinkGenerator linkGenerator = create(activity);
String gplusId = PlusUtils.getCurrentPlusId(activity);
HttpUrl appInviteLink = gplusId != null
? linkGenerator.createAppInviteLink(gplusId)
: NON_SIGNED_IN_INVITE_URL;
ShareCompat.IntentBuilder.from(activity)
.setChooserTitle(R.string.invite_friends)
.setText(activity.getString(R.string.invitation_message, appInviteLink))
.setType("text/plain")
.startChooser();
HttpUrl appInviteLink;
if (gplusId != null) {
appInviteLink = linkGenerator.createAppInviteLink(gplusId);
linkGenerator.shareShortAppInviteLink(appInviteLink.toString());
linkGenerator.showProgressDialog(activity);
} else {
appInviteLink = NON_SIGNED_IN_INVITE_URL;
linkGenerator.createChooser(appInviteLink);
}


activity.sendAnalyticsEvent("AppInvite", "Shared",
gplusId != null ? "Signed In" : "Non Signed In");
Expand All @@ -63,4 +75,39 @@ private String createDeepLink(String gplusId) {
.toString();
}

private void shareShortAppInviteLink(String longUrl) {
FirebaseDynamicLinks firebaseDynamicLinks = App.from(activity).getFirebaseDynamicLinks();
FirebaseDynamicLinksRequest firebaseDynamicLinksRequest =
new FirebaseDynamicLinksRequest(longUrl, new Suffix());
firebaseDynamicLinks.shortenUrl(BuildConfig.IP_SIMPLE_API_ACCESS_KEY, firebaseDynamicLinksRequest)
.enqueue(new Callback<FirebaseDynamicLinksResponse>() {
@Override
public void onSuccess(FirebaseDynamicLinksResponse response) {
createChooser(HttpUrl.parse(response.getShortLink()));
shareProgressDialog.dismiss();
}

@Override
public void onError() {
createChooser(NON_SIGNED_IN_INVITE_URL);
shareProgressDialog.dismiss();
}
});
}

private void showProgressDialog(GdgActivity activity) {
shareProgressDialog = new ProgressDialog(activity);
shareProgressDialog.setIndeterminate(true);
shareProgressDialog.setCancelable(true);
shareProgressDialog.setMessage(activity.getString(R.string.generating_url));
shareProgressDialog.show();
}

private void createChooser(HttpUrl appInviteLink) {
ShareCompat.IntentBuilder.from(activity)
.setChooserTitle(R.string.invite_friends)
.setText(activity.getString(R.string.invitation_message, appInviteLink))
.setType("text/plain")
.startChooser();
}
}
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,5 @@
<string name="invite_congrats">Yey! You\'re invited by\n%s</string>
<string name="friend">a friend</string>
<string name="login_register">Login/Register</string>
<string name="generating_url">Generating URL</string>
</resources>