Skip to content

Commit

Permalink
Use ImageView for background that must not stretch (fix #69)
Browse files Browse the repository at this point in the history
Signed-off-by: Daniele Ricci <daniele.athome@gmail.com>
  • Loading branch information
daniele-athome committed Aug 12, 2014
1 parent eda2147 commit 85f38f8
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 37 deletions.
5 changes: 4 additions & 1 deletion app/src/main/java/org/kontalk/ui/ComposeMessageFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
Expand Down Expand Up @@ -241,8 +242,10 @@ public void onActivityCreated(Bundle savedInstanceState) {
// set custom background (if any)
Drawable bg = Preferences.getConversationBackground(getActivity());
if (bg != null) {
ImageView background = (ImageView) getView().findViewById(R.id.background);
list.setCacheColorHint(Color.TRANSPARENT);
list.setBackgroundDrawable(bg);
list.setBackgroundColor(Color.TRANSPARENT);
background.setImageDrawable(bg);
}

mTextEntry = (EditText) getView().findViewById(R.id.text_editor);
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/java/org/kontalk/ui/PreferencesActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -488,9 +488,13 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
// invalidate any previous reference
Preferences.setCachedCustomBackground(null);
// resize and cache image
// TODO do this in background (might take some time)
File image = Preferences.cacheConversationBackground(this, data.getData());
// save to preferences
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit()
.putString("pref_background_uri", data.getDataString())
.putString("pref_background_uri", Uri.fromFile(image).toString())
.commit();
}
}
Expand Down
52 changes: 49 additions & 3 deletions app/src/main/java/org/kontalk/util/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

package org.kontalk.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.kontalk.R;
import org.kontalk.client.EndpointServer;
Expand All @@ -35,13 +38,16 @@
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.provider.BaseColumns;
import android.text.TextUtils;
import android.view.Display;
import android.view.WindowManager;


/**
Expand Down Expand Up @@ -238,6 +244,48 @@ public static boolean setStatusMessage(Context context, String message) {
.commit();
}

/** Loads and stores a cached version of the given conversation background. */
public static File cacheConversationBackground(Context context, Uri uri) {
InputStream in = null;
OutputStream out = null;
try {
in = context.getContentResolver().openInputStream(uri);

Bitmap bmap = BitmapFactory.decodeStream(in, null, null);

WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();

Bitmap tn = ThumbnailUtils.extractThumbnail(bmap, display.getWidth(), display.getHeight());
bmap.recycle();

File outFile = new File(context.getFilesDir(), "background.png");
out = new FileOutputStream(outFile);
tn.compress(Bitmap.CompressFormat.PNG, 90, out);
tn.recycle();

return outFile;
}
catch (Exception e) {
// ignored
}
finally {
try {
in.close();
}
catch (Exception e) {
// ignored
}
try {
out.close();
}
catch (Exception e) {
// ignored
}
}
return null;
}

public static Drawable getConversationBackground(Context context) {
InputStream in = null;
try {
Expand All @@ -246,9 +294,7 @@ public static Drawable getConversationBackground(Context context) {
String _customBg = getString(context, "pref_background_uri", null);
in = context.getContentResolver().openInputStream(Uri.parse(_customBg));

BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inSampleSize = 4;
Bitmap bmap = BitmapFactory.decodeStream(in, null, opt);
Bitmap bmap = BitmapFactory.decodeStream(in, null, null);
sCustomBackground = new BitmapDrawable(context.getResources(), bmap);
}
return sCustomBackground;
Expand Down
75 changes: 43 additions & 32 deletions app/src/main/res/layout/compose_message.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,48 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<include layout="@layout/invitation_bar"
android:id="@+id/invitation_bar"
android:visibility="gone"/>

<ListView android:id="@android:id/list"
style="?android:attr/listViewWhiteStyle"
android:background="@color/compose_message_list_background"
android:cacheColorHint="@color/compose_message_list_background"
android:divider="@null"
android:dividerHeight="0.0sp"
android:layout_height="fill_parent">

<ImageView android:id="@+id/background"
android:scaleType="centerCrop"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1.0"
android:drawSelectorOnTop="false"
android:transcriptMode="normal"
android:smoothScrollbar="false"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:fastScrollEnabled="true"
android:fastScrollAlwaysVisible="true"
android:scrollbarStyle="insideInset"
android:stackFromBottom="true"
android:fadingEdgeLength="8dp"
>
<!-- Preview: listitem=@layout/message_list_item -->
</ListView>

<include layout="@layout/composer_bar"/>

</LinearLayout>
android:layout_height="fill_parent"
android:orientation="vertical">

<include layout="@layout/invitation_bar"
android:id="@+id/invitation_bar"
android:visibility="gone"/>

<ListView android:id="@android:id/list"
style="?android:attr/listViewWhiteStyle"
android:background="@color/compose_message_list_background"
android:cacheColorHint="@color/compose_message_list_background"
android:divider="@null"
android:dividerHeight="0.0sp"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1.0"
android:drawSelectorOnTop="false"
android:transcriptMode="normal"
android:smoothScrollbar="false"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:fastScrollEnabled="true"
android:fastScrollAlwaysVisible="true"
android:scrollbarStyle="insideInset"
android:stackFromBottom="true"
android:fadingEdgeLength="8dp"
>
<!-- Preview: listitem=@layout/message_list_item -->
</ListView>

<include layout="@layout/composer_bar"/>

</LinearLayout>

</RelativeLayout>

0 comments on commit 85f38f8

Please sign in to comment.