Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
zetbaitsu committed Apr 4, 2017
2 parents c3ac0a0 + 015bf6b commit 71dbc6c
Show file tree
Hide file tree
Showing 11 changed files with 205 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ allprojects {
Then add to your app module build.gradle
```groovy
dependencies {
compile 'com.qiscus.sdk:chat:1.16.1'
compile 'com.qiscus.sdk:chat:1.16.2'
}
```
# Let's make cools chatting apps!
Expand Down
2 changes: 1 addition & 1 deletion chat/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ ext {
siteUrl = 'https://github.com/qiscus/qiscus-sdk-android'
gitUrl = 'https://github.com/qiscus/qiscus-sdk-android.git'

libraryVersion = '1.16.1'
libraryVersion = '1.16.2'

developerId = 'qiscustech'
developerName = 'Qiscus Tech'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,36 @@

package com.qiscus.sdk.filepicker.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.android.internal.util.Predicate;
import com.qiscus.sdk.R;
import com.qiscus.sdk.filepicker.FilePickerConst;
import com.qiscus.sdk.filepicker.PickerManager;
import com.qiscus.sdk.filepicker.adapter.SectionsPagerAdapter;
import com.qiscus.sdk.filepicker.model.Document;
import com.qiscus.sdk.filepicker.model.FileType;
import com.qiscus.sdk.filepicker.util.MediaStoreHelper;
import com.qiscus.sdk.filepicker.util.TabLayoutHelper;
import com.qiscus.sdk.util.QiscusFileUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
Expand All @@ -47,12 +58,17 @@
* GitHub : https://github.com/zetbaitsu
*/
public class DocPickerFragment extends Fragment {
protected static final int PICK_FILE_REQUEST = 363;

private TabLayout tabLayout;
private ViewPager viewPager;
private ProgressBar progressBar;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setHasOptionsMenu(true);
}
return inflater.inflate(R.layout.fragment_qiscus_doc_picker, container, false);
}

Expand Down Expand Up @@ -129,4 +145,54 @@ private <T> Collection<T> filter(Collection<T> target, Predicate<T> predicate) {
}
return result;
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
inflater.inflate(R.menu.file_picker, menu);
}
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int i = item.getItemId();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && i == R.id.action_choose_manually) {
addFile();
}
return super.onOptionsItemSelected(item);
}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
protected void addFile() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
String[] mimeTypes = {"text/plain", "application/pdf",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"application/vnd.openxmlformats-officedocument.presentationml.presentation",
"application/msword", "application/vnd.ms-excel", "application/vnd.ms-powerpoint"};
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
startActivityForResult(intent, PICK_FILE_REQUEST);

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_FILE_REQUEST && resultCode == Activity.RESULT_OK) {
if (data == null) {
Toast.makeText(getActivity(), "Can not open file", Toast.LENGTH_SHORT).show();
return;
}
try {
PickerManager.getInstance()
.add(QiscusFileUtil.from(data.getData()).getAbsolutePath(), FilePickerConst.FILE_TYPE_DOCUMENT);
} catch (IOException e) {
Toast.makeText(getActivity(), "Can not read file", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,45 @@

package com.qiscus.sdk.filepicker.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.qiscus.sdk.R;
import com.qiscus.sdk.filepicker.FilePickerConst;
import com.qiscus.sdk.filepicker.PickerManager;
import com.qiscus.sdk.filepicker.adapter.SectionsPagerAdapter;
import com.qiscus.sdk.util.QiscusFileUtil;

import java.io.IOException;

/**
* Created on : March 16, 2017
* Author : zetbaitsu
* Name : Zetra
* GitHub : https://github.com/zetbaitsu
*/
public class MediaPickerFragment extends Fragment {
public class MediaPickerFragment extends Fragment implements ViewPager.OnPageChangeListener {

protected static final int PICK_IMAGE_REQUEST = 252;
protected static final int PICK_VIDEO_REQUEST = 253;

private int position;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setHasOptionsMenu(true);
return inflater.inflate(R.layout.fragment_qiscus_media_picker, container, false);
}

Expand Down Expand Up @@ -82,6 +97,71 @@ private void initView(View view) {
}

viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(this);
tabLayout.setupWithViewPager(viewPager);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.file_picker, menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int i = item.getItemId();
if (i == R.id.action_choose_manually) {
if (position == 0) {
addImage();
} else {
addVideo();
}
}
return super.onOptionsItemSelected(item);
}

protected void addImage() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, PICK_IMAGE_REQUEST);
}

protected void addVideo() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("video/*");
startActivityForResult(intent, PICK_VIDEO_REQUEST);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if ((requestCode == PICK_IMAGE_REQUEST || requestCode == PICK_VIDEO_REQUEST) && resultCode == Activity.RESULT_OK) {
if (data == null) {
Toast.makeText(getActivity(), "Can not open file", Toast.LENGTH_SHORT).show();
return;
}
try {
PickerManager.getInstance()
.add(QiscusFileUtil.from(data.getData()).getAbsolutePath(), FilePickerConst.FILE_TYPE_MEDIA);
} catch (IOException e) {
Toast.makeText(getActivity(), "Can not read file", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {
this.position = position;
}

@Override
public void onPageScrollStateChanged(int state) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,18 @@
import android.text.method.LinkMovementMethod;
import android.text.method.MovementMethod;
import android.text.style.ClickableSpan;
import android.util.Patterns;
import android.view.View;

import com.qiscus.sdk.Qiscus;
import com.qiscus.sdk.data.model.QiscusComment;
import com.qiscus.sdk.ui.adapter.OnItemClickListener;
import com.qiscus.sdk.ui.adapter.OnLongItemClickListener;
import com.qiscus.sdk.ui.view.QiscusLinkPreviewView;
import com.qiscus.sdk.util.QiscusAndroidUtil;
import com.schinizer.rxunfurl.model.PreviewData;

import java.util.regex.Matcher;

/**
* Created on : December 09, 2016
* Author : zetbaitsu
Expand All @@ -51,6 +53,7 @@ public QiscusBaseLinkViewHolder(View itemView, OnItemClickListener itemClickList
OnLongItemClickListener longItemClickListener) {
super(itemView, itemClickListener, longItemClickListener);
linkPreviewView = getLinkPreviewView(itemView);
messageTextView.setOnLongClickListener(v -> onLongClick(itemView));
}

@NonNull
Expand All @@ -72,9 +75,16 @@ protected void showMessage(QiscusComment qiscusComment) {
}

private void setUpLinks(QiscusComment qiscusComment) {
for (String link : QiscusAndroidUtil.extractPlainUrl(qiscusComment.getMessage())) {
clickify(link, () -> {
String url = link;
String message = qiscusComment.getMessage();
Matcher matcher = Patterns.WEB_URL.matcher(message);
while (matcher.find()) {
int start = matcher.start();
if (start > 0 && message.charAt(start - 1) == '@') {
continue;
}
int end = matcher.end();
clickify(start, end, () -> {
String url = message.substring(start, end);
if (!url.startsWith("http")) {
url = "http://" + url;
}
Expand Down Expand Up @@ -122,13 +132,10 @@ public interface OnClickListener {
}
}

private void clickify(String clickableText, ClickSpan.OnClickListener listener) {
private void clickify(int start, int end, ClickSpan.OnClickListener listener) {
CharSequence text = messageTextView.getText();
String string = text.toString();
ClickSpan span = new ClickSpan(listener);

int start = string.indexOf(clickableText);
int end = start + clickableText.length();
if (start == -1) {
return;
}
Expand Down
27 changes: 10 additions & 17 deletions chat/src/main/java/com/qiscus/sdk/util/QiscusAndroidUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;

/**
* Created on : May 31, 2016
Expand Down Expand Up @@ -69,26 +70,18 @@ public static boolean isUrl(String s) {
}

public static List<String> extractUrl(String text) {
String[] strings = text.split(" ");
List<String> urls = new ArrayList<>();
for (String s : strings) {
if (isUrl(s)) {
if (!s.startsWith("http")) {
s = "http://" + s;
}
urls.add(s);
Matcher matcher = Patterns.WEB_URL.matcher(text);
while (matcher.find()) {
int start = matcher.start();
if (start > 0 && text.charAt(start - 1) == '@') {
continue;
}
}
return urls;
}

public static List<String> extractPlainUrl(String text) {
String[] strings = text.split(" ");
List<String> urls = new ArrayList<>();
for (String s : strings) {
if (isUrl(s)) {
urls.add(s);
String url = matcher.group();
if (!url.startsWith("http")) {
url = "http://" + url;
}
urls.add(url);
}
return urls;
}
Expand Down
2 changes: 2 additions & 0 deletions chat/src/main/res/layout/item_qiscus_chat_link.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@
android:id="@+id/contents"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoLink="email"
android:linksClickable="true"
android:textColor="@color/qiscus_white"
android:textColorLink="@color/qiscus_white"
android:textSize="16sp"
Expand Down
2 changes: 2 additions & 0 deletions chat/src/main/res/layout/item_qiscus_chat_link_me.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
android:id="@+id/contents"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoLink="email"
android:linksClickable="true"
android:textColor="@color/qiscus_primary_text"
android:textColorLink="@color/qiscus_primary_text"
android:textSize="16sp"
Expand Down
2 changes: 1 addition & 1 deletion chat/src/main/res/layout/item_qiscus_chat_text.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:autoLink="web"
android:autoLink="all"
android:linksClickable="true"
android:textColor="@color/qiscus_white"
android:textColorLink="@color/qiscus_white"
Expand Down
2 changes: 1 addition & 1 deletion chat/src/main/res/layout/item_qiscus_chat_text_me.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:autoLink="web"
android:autoLink="all"
android:linksClickable="true"
android:textColor="@color/qiscus_primary_text"
android:textColorLink="@color/qiscus_primary_text"
Expand Down
Loading

0 comments on commit 71dbc6c

Please sign in to comment.