package com.tasomaniac.openwith;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import androidx.core.app.ShareCompat;
import com.tasomaniac.openwith.data.Analytics;
import com.tasomaniac.openwith.redirect.RedirectFixActivity;
import com.tasomaniac.openwith.util.CallerPackageExtractor;
import com.tasomaniac.openwith.util.Urls;
import dagger.android.DaggerActivity;

import javax.annotation.Nullable;
import javax.inject.Inject;

public class ShareToOpenWith extends DaggerActivity {

    public static final String EXTRA_FROM_DIRECT_SHARE = "EXTRA_FROM_DIRECT_SHARE";

    private static boolean isFromDirectShare(Intent intent) {
        return intent.getBooleanExtra(EXTRA_FROM_DIRECT_SHARE, false);
    }

    @Inject Analytics analytics;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Ignore special case for setting OLW as default browser.
        if ("https://".equals(getIntent().getDataString())) {
            finish();
            return;
        }

        analytics.sendScreenView("ShareToOpenWith");
        if (isFromDirectShare(getIntent())) {
            analytics.sendEvent(
                    "Direct Share",
                    "Clicked",
                    "true"
            );
        }

        final ShareCompat.IntentReader reader = ShareCompat.IntentReader.from(this);
        String foundUrl = Urls.extractUrlFrom(getIntent(), reader);

        if (foundUrl != null) {
            String callerPackage = CallerPackageExtractor.from(this).extract();
            Intent intent = RedirectFixActivity.createIntent(this, foundUrl)
                    .putExtra(ShareCompat.EXTRA_CALLING_PACKAGE, callerPackage);
            startActivity(intent);
        } else {
            Toast.makeText(this, R.string.error_invalid_url, Toast.LENGTH_SHORT).show();
        }
        finish();
    }

}

@@ -607,148 +607,8 @@
 %22;%0A%0A
-    private static boolean isFromDirectShare(Intent intent) %7B%0A        return intent.getBooleanExtra(EXTRA_FROM_DIRECT_SHARE, false);%0A    %7D%0A%0A
     
@@ -636,16 +636,16 @@
 lytics;%0A
+
 %0A    @Ov
@@ -1009,197 +1009,27 @@
     
-if (isFromDirectShare(getIntent())) %7B%0A            analytics.sendEvent(%0A                    %22
+track
 Direct
- 
 Share
-%22,%0A                    %22Clicked%22,%0A                    %22true%22%0A            );%0A        %7D
+();
 %0A%0A  
@@ -1208,24 +1208,54 @@
  != null) %7B%0A
+            trackLinkOpen();%0A%0A
             
@@ -1631,16 +1631,16 @@
       %7D%0A
-
         
@@ -1656,10 +1656,707 @@
 %0A    %7D%0A%0A
+    private void trackDirectShare() %7B%0A        if (isFromDirectShare(getIntent())) %7B%0A            analytics.sendEvent(%0A                    %22Direct Share%22,%0A                    %22Clicked%22,%0A                    %22true%22%0A            );%0A        %7D%0A    %7D%0A%0A    private void trackLinkOpen() %7B%0A        analytics.sendEvent(%0A                %22Open Link%22,%0A                %22Set as browser%22,%0A                Boolean.toString(isSetAsBrowser(getIntent()))%0A        );%0A    %7D%0A%0A    private static boolean isFromDirectShare(Intent intent) %7B%0A        return intent.getBooleanExtra(EXTRA_FROM_DIRECT_SHARE, false);%0A    %7D%0A%0A    private static boolean isSetAsBrowser(Intent intent) %7B%0A        return intent.getData() != null;%0A    %7D%0A%0A
 %7D%0A

Add tracking for browser share