Skip to content
This repository was archived by the owner on Feb 10, 2023. It is now read-only.

Commit c1cdf88

Browse files
author
Nenad Lukic
committed
Initial commit
0 parents  commit c1cdf88

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1116
-0
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/
5+
.DS_Store
6+
/build
7+
/captures
8+
.externalNativeBuild

app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/build.gradle

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 25
5+
defaultConfig {
6+
applicationId "com.example.nlukic.webviewtest"
7+
minSdkVersion 15
8+
targetSdkVersion 25
9+
versionCode 1
10+
versionName "1.0"
11+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12+
}
13+
buildTypes {
14+
release {
15+
minifyEnabled false
16+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17+
}
18+
}
19+
}
20+
21+
dependencies {
22+
implementation fileTree(dir: 'libs', include: ['*.jar'])
23+
implementation 'com.android.support:appcompat-v7:25.4.0'
24+
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
25+
implementation 'com.android.support:design:25.4.0'
26+
testImplementation 'junit:junit:4.12'
27+
androidTestImplementation 'com.android.support.test:runner:1.0.1'
28+
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
29+
compile 'com.android.volley:volley:1.1.0'
30+
}

app/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.example.nlukic.webviewtest;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import static org.junit.Assert.*;
11+
12+
/**
13+
* Instrumented test, which will execute on an Android device.
14+
*
15+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
16+
*/
17+
@RunWith(AndroidJUnit4.class)
18+
public class ExampleInstrumentedTest {
19+
@Test
20+
public void useAppContext() throws Exception {
21+
// Context of the app under test.
22+
Context appContext = InstrumentationRegistry.getTargetContext();
23+
24+
assertEquals("com.example.nlukic.webviewtest", appContext.getPackageName());
25+
}
26+
}

app/src/main/AndroidManifest.xml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.example.nlukic.webviewtest">
4+
<uses-permission android:name="android.permission.INTERNET" />
5+
6+
<application
7+
android:allowBackup="true"
8+
android:icon="@mipmap/ic_launcher"
9+
android:label="@string/app_name"
10+
android:roundIcon="@mipmap/ic_launcher_round"
11+
android:supportsRtl="true"
12+
android:theme="@style/AppTheme">
13+
<activity
14+
android:name=".MainActivity"
15+
android:label="@string/app_name"
16+
android:theme="@style/AppTheme.NoActionBar">
17+
<intent-filter>
18+
<action android:name="android.intent.action.MAIN" />
19+
20+
<category android:name="android.intent.category.LAUNCHER" />
21+
</intent-filter>
22+
</activity>
23+
</application>
24+
25+
</manifest>
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package com.example.nlukic.webviewtest;
2+
3+
import android.support.v4.app.Fragment;
4+
import android.os.Bundle;
5+
import android.support.design.widget.FloatingActionButton;
6+
import android.support.v4.app.FragmentTransaction;
7+
import android.support.v7.app.AppCompatActivity;
8+
import android.support.v7.widget.Toolbar;
9+
import android.util.Log;
10+
import android.view.View;
11+
import android.view.Menu;
12+
import android.view.MenuItem;
13+
14+
import com.android.volley.NetworkResponse;
15+
import com.android.volley.VolleyError;
16+
import com.example.nlukic.webviewtest.utils.Requester;
17+
import com.example.nlukic.webviewtest.utils.UserRequestListener;
18+
19+
import org.json.JSONException;
20+
import org.json.JSONObject;
21+
22+
23+
public class MainActivity extends AppCompatActivity implements UserRequestListener {
24+
25+
private String userId;
26+
27+
@Override
28+
protected void onCreate(Bundle savedInstanceState) {
29+
super.onCreate(savedInstanceState);
30+
setContentView(R.layout.activity_main);
31+
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
32+
setSupportActionBar(toolbar);
33+
34+
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
35+
36+
fab.setOnClickListener(new View.OnClickListener() {
37+
@Override
38+
public void onClick(View view) {
39+
40+
// Create new fragment and transaction
41+
Fragment newFragment = new ViewerFragment(MainActivity.this.userId);
42+
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
43+
44+
// Replace whatever is in the fragment_container view with this fragment,
45+
// and add the transaction to the back stack
46+
transaction.replace(R.id.fragment_container, newFragment);
47+
transaction.addToBackStack(null);
48+
49+
// Commit the transaction
50+
transaction.commit();
51+
}
52+
});
53+
54+
try {
55+
(new Requester(this)).getUser();
56+
} catch (JSONException ex) {
57+
Log.v("Requester.Exception", ex.getMessage());
58+
}
59+
60+
// Create new fragment and transaction
61+
MainFragment newFragment = new MainFragment("Click on the fab button to link your account.");
62+
63+
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
64+
65+
// Replace whatever is in the fragment_container view with this fragment,
66+
// and add the transaction to the back stack
67+
transaction.add(R.id.fragment_container, newFragment);
68+
transaction.addToBackStack(null);
69+
70+
// Commit the transaction
71+
transaction.commit();
72+
}
73+
74+
@Override
75+
public void onUserActionSuccess(JSONObject resp) {
76+
try {
77+
JSONObject result = resp.getJSONObject("result");
78+
Log.v("onUserActionSuccess", "User ID: " + result.getString("id"));
79+
this.userId = result.getString("id");
80+
} catch (JSONException ex) {
81+
Log.v("onUserActionSuccess", ex.toString());
82+
}
83+
Log.v("onUserActionSuccess", "Response is: " + resp.toString());
84+
}
85+
86+
@Override
87+
public void onUserActionFailure(VolleyError error) {
88+
NetworkResponse networkResponse = error.networkResponse;
89+
if (networkResponse != null && networkResponse.data != null) {
90+
String jsonError = new String(networkResponse.data);
91+
Log.v("Response.OnError", "Response is: " + jsonError);
92+
}
93+
Log.v("Response.OnError", error.toString());
94+
}
95+
96+
@Override
97+
public boolean onCreateOptionsMenu(Menu menu) {
98+
// Inflate the menu; this adds items to the action bar if it is present.
99+
getMenuInflater().inflate(R.menu.menu_main, menu);
100+
return true;
101+
}
102+
103+
@Override
104+
public boolean onOptionsItemSelected(MenuItem item) {
105+
// Handle action bar item clicks here. The action bar will
106+
// automatically handle clicks on the Home/Up button, so long
107+
// as you specify a parent activity in AndroidManifest.xml.
108+
int id = item.getItemId();
109+
110+
//noinspection SimplifiableIfStatement
111+
if (id == R.id.action_settings) {
112+
return true;
113+
}
114+
115+
return super.onOptionsItemSelected(item);
116+
}
117+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.example.nlukic.webviewtest;
2+
3+
import android.support.v4.app.Fragment;
4+
import android.os.Bundle;
5+
import android.view.LayoutInflater;
6+
import android.view.View;
7+
import android.view.ViewGroup;
8+
import android.widget.TextView;
9+
10+
public class MainFragment extends Fragment {
11+
12+
private String textContent;
13+
14+
public MainFragment() {
15+
}
16+
17+
public MainFragment(String text) {
18+
this.textContent = text;
19+
}
20+
21+
@Override
22+
public View onCreateView(LayoutInflater inflater, ViewGroup container,
23+
Bundle savedInstanceState) {
24+
// Inflate the layout for this fragment
25+
View view = inflater.inflate(R.layout.content_main, container, false);
26+
27+
28+
TextView textView = (TextView) view.findViewById(R.id.textina);
29+
30+
textView.setText(this.textContent);
31+
32+
return view;
33+
}
34+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.example.nlukic.webviewtest;
2+
3+
import android.support.v4.app.Fragment;
4+
import android.os.Bundle;
5+
import android.view.LayoutInflater;
6+
import android.view.View;
7+
import android.view.ViewGroup;
8+
import android.webkit.WebSettings;
9+
import android.webkit.WebView;
10+
import android.webkit.WebViewClient;
11+
12+
public class ViewerFragment extends Fragment {
13+
14+
private String userId;
15+
16+
public ViewerFragment() {
17+
18+
}
19+
20+
public ViewerFragment(String userId) {
21+
this.userId = userId;
22+
}
23+
24+
@Override
25+
public View onCreateView(LayoutInflater inflater, ViewGroup container,
26+
Bundle savedInstanceState) {
27+
// Inflate the layout for this fragment
28+
View view = inflater.inflate(R.layout.webview_layout, container, false);
29+
30+
WebView myWebView = (WebView) view.findViewById(R.id.webview);
31+
myWebView.loadUrl("http://192.168.2.97?user_id="+this.userId);
32+
//myWebView.loadUrl("http://www.google.com");
33+
WebSettings webSettings = myWebView.getSettings();
34+
webSettings.setJavaScriptEnabled(true);
35+
webSettings.setDomStorageEnabled(true);
36+
myWebView.setWebViewClient(new WebViewClient());
37+
myWebView.addJavascriptInterface(new WebAppInterface(getActivity()), "Android");
38+
39+
return view;
40+
}
41+
42+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.example.nlukic.webviewtest;
2+
3+
4+
import android.app.Activity;
5+
import android.content.Context;
6+
import android.support.v4.app.FragmentTransaction;
7+
import android.support.v7.app.AppCompatActivity;
8+
import android.webkit.JavascriptInterface;
9+
import android.widget.TextView;
10+
11+
public class WebAppInterface {
12+
Context mContext;
13+
14+
/**
15+
* Instantiate the interface and set the context
16+
*/
17+
WebAppInterface(Context c) {
18+
mContext = c;
19+
}
20+
21+
@JavascriptInterface
22+
public void setConnectionId(String token) {
23+
transitionToFragment(token);
24+
}
25+
26+
@JavascriptInterface
27+
public void cancelActions() {
28+
transitionToFragment("Click on the fab button to link your account.");
29+
}
30+
31+
private void transitionToFragment(String text) {
32+
AppCompatActivity activity = ((AppCompatActivity) mContext);
33+
34+
MainFragment newFragment = new MainFragment(text);
35+
36+
FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
37+
38+
// Replace whatever is in the fragment_container view with this fragment,
39+
// and add the transaction to the back stack
40+
transaction.replace(R.id.fragment_container, newFragment);
41+
transaction.addToBackStack(null);
42+
43+
// Commit the transaction
44+
transaction.commit();
45+
}
46+
}

0 commit comments

Comments
 (0)