1#!/usr/bin/python
2
3# Copyright (C) 2014 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17import os
18import sys
19import re
20
21print "Generate v4 fragment related code for leanback"
22
23cls = ['Base', 'BaseRow', 'Browse', 'Details', 'Error', 'Headers',
24      'PlaybackOverlay', 'Playback', 'Rows', 'Search', 'VerticalGrid', 'Branded',
25      'GuidedStep', 'Onboarding', 'Video']
26
27for w in cls:
28    print "copy {}Fragment to {}SupportFragment".format(w, w)
29
30    file = open('src/android/support/v17/leanback/app/{}Fragment.java'.format(w), 'r')
31    outfile = open('src/android/support/v17/leanback/app/{}SupportFragment.java'.format(w), 'w')
32
33    outfile.write("// CHECKSTYLE:OFF Generated code\n")
34    outfile.write("/* This file is auto-generated from {}Fragment.java.  DO NOT MODIFY. */\n\n".format(w))
35
36    for line in file:
37        line = line.replace('IS_FRAMEWORK_FRAGMENT = true', 'IS_FRAMEWORK_FRAGMENT = false');
38        for w in cls:
39            line = line.replace('{}Fragment'.format(w), '{}SupportFragment'.format(w))
40        line = line.replace('android.app.Fragment', 'android.support.v4.app.Fragment')
41        line = line.replace('android.app.Activity', 'android.support.v4.app.FragmentActivity')
42        line = line.replace('activity.getFragmentManager()', 'activity.getSupportFragmentManager()')
43        line = line.replace('Activity activity', 'FragmentActivity activity')
44        line = line.replace('(Activity', '(FragmentActivity')
45        line = re.sub(r'FragmentUtil.getContext\(.*this\)', 'getContext()', line);
46        outfile.write(line)
47    file.close()
48    outfile.close()
49
50print "copy VideoFragmentGlueHost to VideoSupportFragmentGlueHost".format(w, w)
51file = open('src/android/support/v17/leanback/app/VideoFragmentGlueHost.java'.format(w), 'r')
52outfile = open('src/android/support/v17/leanback/app/VideoSupportFragmentGlueHost.java'.format(w), 'w')
53
54outfile.write("// CHECKSTYLE:OFF Generated code\n")
55outfile.write("/* This file is auto-generated from {}VideoFragmentGlueHost.java.  DO NOT MODIFY. */\n\n".format(w))
56
57for line in file:
58    line = line.replace('IS_FRAMEWORK_FRAGMENT = true', 'IS_FRAMEWORK_FRAGMENT = false');
59    line = line.replace('VideoSupportFragmentGlueHost'.format(w), 'VideoSupportFragmentGlueHost'.format(w))
60    line = line.replace('android.app.Fragment', 'android.support.v4.app.Fragment')
61    line = line.replace('android.app.Activity', 'android.support.v4.app.FragmentActivity')
62    line = line.replace('activity.getFragmentManager()', 'activity.getSupportFragmentManager()')
63    line = line.replace('Activity activity', 'FragmentActivity activity')
64    line = line.replace('VideoFragment', 'VideoSupportFragment')
65    line = line.replace('PlaybackFragmentGlueHost', 'PlaybackSupportFragmentGlueHost')
66    line = line.replace('(Activity', '(FragmentActivity')
67    outfile.write(line)
68file.close()
69outfile.close()
70
71print "copy PlaybackFragmentGlueHost to PlaybackSupportFragmentGlueHost".format(w, w)
72file = open('src/android/support/v17/leanback/app/PlaybackFragmentGlueHost.java'.format(w), 'r')
73outfile = open('src/android/support/v17/leanback/app/PlaybackSupportFragmentGlueHost.java'.format(w), 'w')
74
75outfile.write("// CHECKSTYLE:OFF Generated code\n")
76outfile.write("/* This file is auto-generated from {}PlaybackFragmentGlueHost.java.  DO NOT MODIFY. */\n\n".format(w))
77
78for line in file:
79    line = line.replace('IS_FRAMEWORK_FRAGMENT = true', 'IS_FRAMEWORK_FRAGMENT = false');
80    line = line.replace('VideoSupportFragmentGlueHost'.format(w), 'VideoSupportFragmentGlueHost'.format(w))
81    line = line.replace('android.app.Fragment', 'android.support.v4.app.Fragment')
82    line = line.replace('android.app.Activity', 'android.support.v4.app.FragmentActivity')
83    line = line.replace('activity.getFragmentManager()', 'activity.getSupportFragmentManager()')
84    line = line.replace('Activity activity', 'FragmentActivity activity')
85    line = line.replace('PlaybackFragment', 'PlaybackSupportFragment')
86    line = line.replace('PlaybackFragmentGlueHost', 'PlaybackSupportFragmentGlueHost')
87    line = line.replace('(Activity', '(FragmentActivity')
88    outfile.write(line)
89file.close()
90outfile.close()
91
92print "copy DetailsFragmentBackgroundController to DetailsSupportFragmentBackgroundController".format(w, w)
93file = open('src/android/support/v17/leanback/app/DetailsFragmentBackgroundController.java'.format(w), 'r')
94outfile = open('src/android/support/v17/leanback/app/DetailsSupportFragmentBackgroundController.java'.format(w), 'w')
95
96outfile.write("// CHECKSTYLE:OFF Generated code\n")
97outfile.write("/* This file is auto-generated from {}DetailsFragmentBackgroundController.java.  DO NOT MODIFY. */\n\n".format(w))
98
99for line in file:
100    line = re.sub(r'FragmentUtil.getContext\(mFragment\)', 'mFragment.getContext()', line);
101    line = line.replace('VideoFragment', 'VideoSupportFragment')
102    line = line.replace('DetailsFragment', 'DetailsSupportFragment')
103    line = line.replace('RowsFragment', 'RowsSupportFragment')
104    line = line.replace('VideoSupportFragmentGlueHost'.format(w), 'VideoSupportFragmentGlueHost'.format(w))
105    line = line.replace('android.app.Fragment', 'android.support.v4.app.Fragment')
106    outfile.write(line)
107file.close()
108outfile.close()
109