diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cea3ef5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +*.iml +.gradle +/local.properties +/.idea +/.idea/workspace.xml +/.idea/libraries +.DS_Store +/build +/captures +.externalNativeBuild diff --git a/README.md b/README.md new file mode 100644 index 0000000..3c2ceef --- /dev/null +++ b/README.md @@ -0,0 +1,72 @@ +# Android Link Preview + +An Android Library with demo application, to fetch meta-data from url, like Facebook and Whatsapp .... + +## Installation + +Add it in your root build.gradle at the end of repositories: + +``` + allprojects { + repositories { + ... + maven { url 'https://jitpack.io' } + } + } +``` +Add the dependency +``` + dependencies { + compile 'com.github.omegaes:Android-Link-Preview:1.0.0' + } +```` + + +## Usage + +Request metadata for url, it will execute in background thread, you will get an instance from LinkPreview +or an exception, keep in your mind that onSuccess, onFailed execute in background thread, use handler to access UI objects + +``` +LinkUtil.getInstance().getLinkPreview(Context, String url,GetLinkPreviewListener) +``` + +LinkPreview + +``` +class LinkPreview { + + String title; + String description; + String link; + String siteName; + File imageFile; + +} +``` + +GetLinkPreviewListener +``` +interface { + void onSuccess(LinkPreview link); + void onFailed(Exception e); +} +``` + + +## Contributing + +1. Fork it! +2. Create your feature branch: `git checkout -b my-new-feature` +3. Commit your changes: `git commit -am 'Add some feature'` +4. Push to the branch: `git push origin my-new-feature` +5. Submit a pull request :D + +## History + +Not yet + +## Author + +* **Abdulrahman Babil** - *Software engineer* - [Mega4Tech](http://mega4tech.com) + diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..447dd09 --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,38 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 25 + buildToolsVersion "25.0.0" + defaultConfig { + applicationId "com.mega4tech.androidlinkpreview" + minSdkVersion 14 + targetSdkVersion 25 + versionCode 1 + versionName "1.0" + testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +repositories { + mavenCentral() // jcenter() works as well because it pulls from Maven Central +} + +dependencies { + compile fileTree(dir: 'libs', include: ['*.jar']) + androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { + exclude group: 'com.android.support', module: 'support-annotations' + }) + compile 'com.android.support:appcompat-v7:25.2.0' + testCompile 'junit:junit:4.12' + compile project(':linkpreview') + + compile 'com.github.bumptech.glide:glide:3.7.0' + compile 'com.android.support:support-v4:25.2.0' + +} diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..c79be4a --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,17 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in /Users/aboodba/Library/Android/sdk/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class title to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} diff --git a/app/src/androidTest/java/com/mega4tech/androidlinkpreview/ExampleInstrumentedTest.java b/app/src/androidTest/java/com/mega4tech/androidlinkpreview/ExampleInstrumentedTest.java new file mode 100644 index 0000000..6dceab3 --- /dev/null +++ b/app/src/androidTest/java/com/mega4tech/androidlinkpreview/ExampleInstrumentedTest.java @@ -0,0 +1,26 @@ +package com.mega4tech.androidlinkpreview; + +import android.content.Context; +import android.support.test.InstrumentationRegistry; +import android.support.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumentation test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() throws Exception { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getTargetContext(); + + assertEquals("com.mega4tech.androidlinkpreview", appContext.getPackageName()); + } +} diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..0f7028d --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/mega4tech/androidlinkpreview/MainActivity.java b/app/src/main/java/com/mega4tech/androidlinkpreview/MainActivity.java new file mode 100644 index 0000000..962d7e1 --- /dev/null +++ b/app/src/main/java/com/mega4tech/androidlinkpreview/MainActivity.java @@ -0,0 +1,98 @@ +package com.mega4tech.androidlinkpreview; + +import android.content.Intent; +import android.net.Uri; +import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; +import android.util.Log; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.ImageView; +import android.widget.LinearLayout; +import android.widget.ProgressBar; +import android.widget.RelativeLayout; +import android.widget.TextView; +import android.widget.Toast; + +import com.bumptech.glide.Glide; +import com.mega4tech.linkpreview.GetLinkPreviewListener; +import com.mega4tech.linkpreview.LinkPreview; +import com.mega4tech.linkpreview.LinkUtil; + +public class MainActivity extends AppCompatActivity { + + private LinearLayout activityMain; + private EditText linkEt; + private Button linkFetchBtn; + private RelativeLayout previewGroup; + private ImageView imgPreviewIv; + private TextView titleTv; + private TextView descTv; + private TextView siteTv; + private ProgressBar progress; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + initView(); + + linkFetchBtn.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + progress.setVisibility(View.VISIBLE); + previewGroup.setVisibility(View.GONE); + LinkUtil.getInstance().getLinkPreview(MainActivity.this, linkEt.getText().toString(), new GetLinkPreviewListener() { + @Override + public void onSuccess(final LinkPreview link) { + + runOnUiThread(new Runnable() { + @Override + public void run() { + progress.setVisibility(View.GONE); + previewGroup.setVisibility(View.VISIBLE); + titleTv.setText(link.getTitle() != null ? link.getTitle() : "" ); + + descTv.setText(link.getDescription() != null ? link.getDescription() : "" ); + siteTv.setText(link.getSiteName() != null ? link.getSiteName() : "" ); + previewGroup.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(link.getLink())); + startActivity(browserIntent); + } + }); + if(link.getImageFile() != null) + Glide.with(MainActivity.this).load(link.getImageFile()).into(imgPreviewIv); + else + Glide.with(MainActivity.this).load(R.mipmap.ic_launcher).into(imgPreviewIv); + } + }); + + } + + @Override + public void onFailed(Exception e) { + + progress.setVisibility(View.GONE); + previewGroup.setVisibility(View.VISIBLE); + Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show(); + } + }); + } + }); + } + + private void initView() { + activityMain = (LinearLayout) findViewById(R.id.activity_main); + linkEt = (EditText) findViewById(R.id.link_et); + linkFetchBtn = (Button) findViewById(R.id.link_fetch_btn); + previewGroup = (RelativeLayout) findViewById(R.id.preview_group); + imgPreviewIv = (ImageView) findViewById(R.id.img_preview_iv); + titleTv = (TextView) findViewById(R.id.title_tv); + descTv = (TextView) findViewById(R.id.desc_tv); + siteTv = (TextView) findViewById(R.id.site_tv); + progress = (ProgressBar) findViewById(R.id.progress); + } +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..a976a85 --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,89 @@ + + + + + + + + +